Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 6, 2022
1 parent 80d1450 commit 41ed212
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/shared/watchArray/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ category: Watch

# watchArray

`watchArray` watches for the additions and removals in a list.
Watch for an array with additions and removals.

## Usage

Expand Down
18 changes: 15 additions & 3 deletions packages/shared/watchArray/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ import { unref, watch } from 'vue-demi'

export declare type WatchArrayCallback<V = any, OV = any> = (value: V, oldValue: OV, added: V, removed: OV, onCleanup: (cleanupFn: () => void) => void) => any

export function watchArray<T, Immediate extends Readonly<boolean> = false>(source: WatchSource<T[]>, cb: WatchArrayCallback<T[], Immediate extends true ? T[] | undefined : T[]>, options?: WatchOptions<Immediate>) {
let oldList: T[] = options?.immediate ? [] : [...(source instanceof Function ? source() : unref(source))]
watch(source, (newList, _, onCleanup) => {
/**
* Watch for an array with additions and removals.
*
* @see https://vueuse.org/watchArray
*/
export function watchArray<T, Immediate extends Readonly<boolean> = false>(
source: WatchSource<T[]>,
cb: WatchArrayCallback<T[], Immediate extends true ? T[] | undefined : T[]>,
options?: WatchOptions<Immediate>,
) {
let oldList: T[] = options?.immediate
? []
: [...(source instanceof Function ? source() : unref(source))]

return watch(source, (newList, _, onCleanup) => {
const oldListRemains = new Array<boolean>(oldList.length)
const added: T[] = []
for (const obj of newList) {
Expand Down

0 comments on commit 41ed212

Please sign in to comment.