Skip to content

Commit

Permalink
fix(watch): watched previous values can't be destructure on first fir…
Browse files Browse the repository at this point in the history
…e. (#727)
  • Loading branch information
ygj6 committed Jun 17, 2021
1 parent 0b6ab25 commit b3ab6f9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/apis/watch.ts
Expand Up @@ -329,7 +329,8 @@ function createWatcher(
// The subsequent callbacks will redirect to `callback`.
let shiftCallback = (n: any, o: any) => {
shiftCallback = originalCallback
applyCb(n, o)
// o is undefined on the first call
applyCb(n, isArray(n) ? [] : o)
}
callback = (n: any, o: any) => {
shiftCallback(n, o)
Expand Down
6 changes: 3 additions & 3 deletions test/apis/watch.spec.js
Expand Up @@ -457,7 +457,7 @@ describe('api/watch', () => {
template: `<div>{{obj1.a}} {{obj2.a}}</div>`,
}).$mount()
expect(spy).toBeCalledTimes(1)
expect(spy).toHaveBeenLastCalledWith([1, 2], undefined)
expect(spy).toHaveBeenLastCalledWith([1, 2], [])
obj1.a = 2
obj2.a = 3

Expand Down Expand Up @@ -491,7 +491,7 @@ describe('api/watch', () => {
template: `<div>{{a}} {{b}}</div>`,
}).$mount()
expect(spy).toBeCalledTimes(1)
expect(spy).toHaveBeenLastCalledWith([1, 1], undefined)
expect(spy).toHaveBeenLastCalledWith([1, 1], [])
vm.a = 2
expect(spy).toBeCalledTimes(1)
waitForUpdate(() => {
Expand Down Expand Up @@ -553,7 +553,7 @@ describe('api/watch', () => {
},
})
expect(spy).toBeCalledTimes(1)
expect(spy).toHaveBeenLastCalledWith([1, 1], undefined)
expect(spy).toHaveBeenLastCalledWith([1, 1], [])
vm.a = 2
expect(spy).toBeCalledTimes(2)
expect(spy).toHaveBeenLastCalledWith([2, 1], [1, 1])
Expand Down

0 comments on commit b3ab6f9

Please sign in to comment.