From 55a0a20529570313f4003a6bc057def25346fe95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?null=E4=BB=94?= <308241863@qq.com> Date: Mon, 12 Jul 2021 18:00:30 +0800 Subject: [PATCH] fix(watch): traverse refs in deep watch (#753) Co-authored-by: webfansplz <> --- src/apis/watch.ts | 5 +++++ test/v3/runtime-core/apiWatch.spec.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/apis/watch.ts b/src/apis/watch.ts index 67529575..5073a603 100644 --- a/src/apis/watch.ts +++ b/src/apis/watch.ts @@ -332,6 +332,11 @@ function createWatcher( ) } + if (deep) { + const baseGetter = getter + getter = () => traverse(baseGetter()) + } + const applyCb = (n: any, o: any) => { // cleanup before running cb again runCleanup() diff --git a/test/v3/runtime-core/apiWatch.spec.ts b/test/v3/runtime-core/apiWatch.spec.ts index b98d95b3..6fde5b24 100644 --- a/test/v3/runtime-core/apiWatch.spec.ts +++ b/test/v3/runtime-core/apiWatch.spec.ts @@ -531,4 +531,23 @@ describe('api: watch', () => { expect(data2.value).toMatchObject([1]) }) + + it('watching deep ref', async () => { + const count = ref(0) + const double = computed(() => count.value * 2) + const state = reactive([count, double]) + + let dummy + watch( + () => state, + (state) => { + dummy = [state[0].value, state[1].value] + }, + { deep: true } + ) + + count.value++ + await nextTick() + expect(dummy).toEqual([1, 2]) + }) })