Skip to content

Commit b3ab6f9

Browse files
authored
fix(watch): watched previous values can't be destructure on first fire. (#727)
1 parent 0b6ab25 commit b3ab6f9

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/apis/watch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ function createWatcher(
329329
// The subsequent callbacks will redirect to `callback`.
330330
let shiftCallback = (n: any, o: any) => {
331331
shiftCallback = originalCallback
332-
applyCb(n, o)
332+
// o is undefined on the first call
333+
applyCb(n, isArray(n) ? [] : o)
333334
}
334335
callback = (n: any, o: any) => {
335336
shiftCallback(n, o)

test/apis/watch.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ describe('api/watch', () => {
457457
template: `<div>{{obj1.a}} {{obj2.a}}</div>`,
458458
}).$mount()
459459
expect(spy).toBeCalledTimes(1)
460-
expect(spy).toHaveBeenLastCalledWith([1, 2], undefined)
460+
expect(spy).toHaveBeenLastCalledWith([1, 2], [])
461461
obj1.a = 2
462462
obj2.a = 3
463463

@@ -491,7 +491,7 @@ describe('api/watch', () => {
491491
template: `<div>{{a}} {{b}}</div>`,
492492
}).$mount()
493493
expect(spy).toBeCalledTimes(1)
494-
expect(spy).toHaveBeenLastCalledWith([1, 1], undefined)
494+
expect(spy).toHaveBeenLastCalledWith([1, 1], [])
495495
vm.a = 2
496496
expect(spy).toBeCalledTimes(1)
497497
waitForUpdate(() => {
@@ -553,7 +553,7 @@ describe('api/watch', () => {
553553
},
554554
})
555555
expect(spy).toBeCalledTimes(1)
556-
expect(spy).toHaveBeenLastCalledWith([1, 1], undefined)
556+
expect(spy).toHaveBeenLastCalledWith([1, 1], [])
557557
vm.a = 2
558558
expect(spy).toBeCalledTimes(2)
559559
expect(spy).toHaveBeenLastCalledWith([2, 1], [1, 1])

0 commit comments

Comments
 (0)