New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(watchArray): new function #1705
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2c86d37
feat: implement watchListChanges
w1ndy 6ba0d05
Update packages/shared/watchListChanges/index.md
w1ndy 80d1450
rename to watchArray and add tests
w1ndy 41ed212
chore: update
antfu 0ef0b5e
chore: fix types
antfu 354d201
chore: fix
antfu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
category: Watch | ||
--- | ||
|
||
# watchArray | ||
|
||
Watch for an array with additions and removals. | ||
|
||
## Usage | ||
|
||
Similar to `watch`, but provides the added and removed elements to the callback function. Pass `{ deep: true }` if the list is updated in place with `push`, `splice`, etc. | ||
|
||
```ts | ||
import { watchArray } from '@vueuse/core' | ||
|
||
const list = ref([1, 2, 3]) | ||
|
||
watchArray(list, (newList, oldList, added, removed) => { | ||
console.log(newList) // [1, 2, 3, 4] | ||
console.log(oldList) // [1, 2, 3] | ||
console.log(added) // [4] | ||
console.log(removed) // [] | ||
}) | ||
|
||
onMounted(() => { | ||
list.value = [...list.value, 4] | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { nextTick, reactive, ref } from 'vue-demi' | ||
import { watchArray } from '.' | ||
|
||
describe('watchArray', () => { | ||
it('should work when two lists are different', async () => { | ||
const spy = vitest.fn((newList, oldList, added, removed) => { | ||
expect(newList).toEqual([1, 1, 4]) | ||
expect(oldList).toEqual([1, 2, 3]) | ||
expect(added).toEqual([1, 4]) | ||
expect(removed).toEqual([2, 3]) | ||
}) | ||
|
||
const num = ref([1, 2, 3]) | ||
watchArray(num, spy) | ||
num.value = [1, 1, 4] | ||
await nextTick() | ||
expect(spy).toBeCalledTimes(1) | ||
}) | ||
|
||
it('should work when two lists are identical', async () => { | ||
const spy = vitest.fn((newList, oldList, added, removed) => { | ||
expect(newList).toEqual([1, 2, 3]) | ||
expect(oldList).toEqual([1, 2, 3]) | ||
expect(added).toEqual([]) | ||
expect(removed).toEqual([]) | ||
}) | ||
|
||
const num = ref([1, 2, 3]) | ||
watchArray(num, spy) | ||
num.value = [1, 2, 3] | ||
await nextTick() | ||
expect(spy).toBeCalledTimes(1) | ||
}) | ||
|
||
it('should work with list push', async () => { | ||
const spy = vitest.fn((newList, oldList, added, removed) => { | ||
expect(newList).toEqual([1, 2, 3, 4]) | ||
expect(oldList).toEqual([1, 2, 3]) | ||
expect(added).toEqual([4]) | ||
expect(removed).toEqual([]) | ||
}) | ||
|
||
const num = ref([1, 2, 3]) | ||
watchArray(num, spy, { deep: true }) | ||
num.value.push(4) | ||
await nextTick() | ||
expect(spy).toBeCalledTimes(1) | ||
}) | ||
|
||
it('should work with list splice', async () => { | ||
const spy = vitest.fn((newList, oldList, added, removed) => { | ||
expect(newList).toEqual([1, 5, 6, 7, 3]) | ||
expect(oldList).toEqual([1, 2, 3]) | ||
expect(added).toEqual([5, 6, 7]) | ||
expect(removed).toEqual([2]) | ||
}) | ||
|
||
const num = ref([1, 2, 3]) | ||
watchArray(num, spy, { deep: true }) | ||
num.value.splice(1, 1, 5, 6, 7) | ||
await nextTick() | ||
expect(spy).toBeCalledTimes(1) | ||
}) | ||
|
||
it('should work with reactive source', async () => { | ||
const spy = vitest.fn((newList, oldList, added, removed) => { | ||
expect(newList).toEqual([1, 2, 3, 4]) | ||
expect(oldList).toEqual([1, 2, 3]) | ||
expect(added).toEqual([4]) | ||
expect(removed).toEqual([]) | ||
}) | ||
|
||
const num = reactive([1, 2, 3]) | ||
watchArray(num, spy) | ||
num.push(4) | ||
await nextTick() | ||
expect(spy).toBeCalledTimes(1) | ||
}) | ||
|
||
it('should work with functional source', async () => { | ||
const spy = vitest.fn((newList, oldList, added, removed) => { | ||
expect(newList).toEqual([1, 2, 3, 4]) | ||
expect(oldList).toEqual([1, 2, 3]) | ||
expect(added).toEqual([4]) | ||
expect(removed).toEqual([]) | ||
}) | ||
|
||
const num = ref([1, 2, 3]) | ||
watchArray(() => num.value, spy) | ||
num.value = [...num.value, 4] | ||
await nextTick() | ||
expect(spy).toBeCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { WatchOptions, WatchSource } from 'vue-demi' | ||
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 | ||
|
||
/** | ||
* 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[]> | T[], | ||
cb: WatchArrayCallback<T[], Immediate extends true ? T[] | undefined : T[]>, | ||
options?: WatchOptions<Immediate>, | ||
) { | ||
let oldList: T[] = options?.immediate | ||
? [] | ||
: [...(source instanceof Function | ||
? source() | ||
: Array.isArray(source) | ||
? source | ||
: unref(source)), | ||
] | ||
|
||
return watch(source as WatchSource<T[]>, (newList, _, onCleanup) => { | ||
const oldListRemains = new Array<boolean>(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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
added
andremoved
also have meta data like[{ value: 4, index: 3 }]
?