β¬ οΈ Back to Table of Contents
π watchArray¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 1 |
| π¦ Imports | 4 |
| π’ Vue Composition API | 1 |
| π Type Aliases | 1 |
π Table of Contents¶
π οΈ File Location:¶
π packages/shared/watchArray/index.ts
π¦ Imports¶
| Name | Source |
|---|---|
WatchOptions |
vue |
WatchSource |
vue |
toValue |
vue |
watch |
vue |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
watch |
watch | none | none |
Functions¶
watchArray(source: WatchSource<T[]> | T[], cb: WatchArrayCallback<T[], Immediate extenβ¦, options: WatchOptions<Immediate>): any¶
Watch for an array with additions and removals.
See: https://vueuse.org/watchArray
Raw JSDoc
Calls:
sourceArray.isArraytoValue (from vue)watch (from vue)Array.fromadded.pusholdList.filtercb
Code
export function watchArray<T, Immediate extends Readonly<boolean> = false>(
source: WatchSource<T[]> | T[],
cb: WatchArrayCallback<T[], Immediate extends true ? T[] | undefined : T[]>,
options?: WatchOptions<Immediate>,
) {
let oldList: T[] = options?.immediate
? []
: [...(typeof source === 'function'
? source()
: Array.isArray(source)
? source
: toValue(source))]
return watch(source as WatchSource<T[]>, (newList, _, onCleanup) => {
const oldListRemains = Array.from({ length: oldList.length })
const added: T[] = []
for (const obj of newList) {
let found = false
for (let i = 0; i < oldList.length; i++) {
if (!oldListRemains[i] && obj === oldList[i]) {
oldListRemains[i] = true
found = true
break
}
}
if (!found)
added.push(obj)
}
const removed = oldList.filter((_, i) => !oldListRemains[i])
cb(newList, oldList, added, removed, onCleanup)
oldList = [...newList]
}, options)
}
Type Aliases¶
WatchArrayCallback<V = any, OV = any>¶
type WatchArrayCallback<V = any, OV = any> = (value: V, oldValue: OV, added: V, removed: OV, onCleanup: (cleanupFn: () => void) => void) => any;
Generated by Syntax Scribe