From 91a7db9d9fdf44a6a1af4c6938b914ef370d30ac Mon Sep 17 00:00:00 2001 From: ygj6 Date: Tue, 8 Jun 2021 15:19:50 +0800 Subject: [PATCH 1/2] fix(watch): watched previous values can't be destructure on first fire --- src/apis/watch.ts | 1 + test/apis/watch.spec.js | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/apis/watch.ts b/src/apis/watch.ts index 3f614141..6a2b27e6 100644 --- a/src/apis/watch.ts +++ b/src/apis/watch.ts @@ -329,6 +329,7 @@ function createWatcher( // The subsequent callbacks will redirect to `callback`. let shiftCallback = (n: any, o: any) => { shiftCallback = originalCallback + o = isArray(n) ? [] : undefined applyCb(n, o) } callback = (n: any, o: any) => { diff --git a/test/apis/watch.spec.js b/test/apis/watch.spec.js index 33f5b626..eeb323e6 100644 --- a/test/apis/watch.spec.js +++ b/test/apis/watch.spec.js @@ -457,7 +457,7 @@ describe('api/watch', () => { template: `
{{obj1.a}} {{obj2.a}}
`, }).$mount() expect(spy).toBeCalledTimes(1) - expect(spy).toHaveBeenLastCalledWith([1, 2], undefined) + expect(spy).toHaveBeenLastCalledWith([1, 2], []) obj1.a = 2 obj2.a = 3 @@ -491,7 +491,7 @@ describe('api/watch', () => { template: `
{{a}} {{b}}
`, }).$mount() expect(spy).toBeCalledTimes(1) - expect(spy).toHaveBeenLastCalledWith([1, 1], undefined) + expect(spy).toHaveBeenLastCalledWith([1, 1], []) vm.a = 2 expect(spy).toBeCalledTimes(1) waitForUpdate(() => { @@ -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]) From 2dc71453e4a02a5c7500e70407b593b93d0b24ae Mon Sep 17 00:00:00 2001 From: ygj6 Date: Tue, 8 Jun 2021 20:40:24 +0800 Subject: [PATCH 2/2] update the implementation mode --- src/apis/watch.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/apis/watch.ts b/src/apis/watch.ts index 6a2b27e6..c3cb5a88 100644 --- a/src/apis/watch.ts +++ b/src/apis/watch.ts @@ -329,8 +329,8 @@ function createWatcher( // The subsequent callbacks will redirect to `callback`. let shiftCallback = (n: any, o: any) => { shiftCallback = originalCallback - o = isArray(n) ? [] : undefined - applyCb(n, o) + // o is undefined on the first call + applyCb(n, isArray(n) ? [] : o) } callback = (n: any, o: any) => { shiftCallback(n, o)