From: Carlos Rodrigues Date: Fri, 17 Apr 2020 13:55:41 +0000 (+0100) Subject: fix(watch): fix deep watchers on refs containing primitives (#984) X-Git-Tag: v3.0.0-beta.2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99fd158d090594a433b57d9ff9420f3aed48ad2d;p=thirdparty%2Fvuejs%2Fcore.git fix(watch): fix deep watchers on refs containing primitives (#984) --- diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index f42640abc4..8b58ded390 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -86,6 +86,23 @@ describe('api: watch', () => { expect(dummy).toMatchObject([2, 1]) }) + it('watching primitive with deep: true', async () => { + const count = ref(0) + let dummy + watch( + count, + (c, prevCount) => { + dummy = [c, prevCount] + }, + { + deep: true + } + ) + count.value++ + await nextTick() + expect(dummy).toMatchObject([1, 0]) + }) + it('watching multiple sources', async () => { const state = reactive({ count: 1 }) const count = ref(1) diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index ff3e7c6257..1c02c115e9 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -286,6 +286,9 @@ export function instanceWatch( function traverse(value: unknown, seen: Set = new Set()) { if (!isObject(value) || seen.has(value)) { + return value + } + if (seen.has(value)) { return } seen.add(value)