From c49a6ebf924ecb63dea533c1f0ee2e02ab12ea93 Mon Sep 17 00:00:00 2001 From: Yang Mingshan Date: Sun, 23 Aug 2020 16:50:59 +0800 Subject: [PATCH] fix(watch): deep watch --- .../runtime-core/__tests__/apiWatch.spec.ts | 19 +++++++++++++++++++ packages/runtime-core/src/apiWatch.ts | 4 +++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index c315de9713e..138a3e04f5d 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -574,6 +574,25 @@ describe('api: watch', () => { expect(dummy).toEqual([1, 2, 2, false]) }) + 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]) + }) + it('immediate', async () => { const count = ref(0) const cb = jest.fn() diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index ac797b89ea3..9c131067feb 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -329,7 +329,9 @@ function traverse(value: unknown, seen: Set = new Set()) { return value } seen.add(value) - if (isArray(value)) { + if (isRef(value)) { + traverse(value.value, seen) + } else if (isArray(value)) { for (let i = 0; i < value.length; i++) { traverse(value[i], seen) }