diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 263864df..d8854f0a 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -261,34 +261,45 @@ describe('api: watch', () => { count: ref(0) }, array: [1, 2, 3], - map: new Map([['a', 1], ['b', 2]]) + map: new Map([['a', 1], ['b', 2]]), + set: new Set([1, 2, 3]) }) let dummy watch( () => state, state => { - dummy = [state.nested.count, state.array[0], state.map.get('a')] + dummy = [ + state.nested.count, + state.array[0], + state.map.get('a'), + state.set.has(1) + ] }, { deep: true } ) await nextTick() - expect(dummy).toEqual([0, 1, 1]) + expect(dummy).toEqual([0, 1, 1, true]) state.nested.count++ await nextTick() - expect(dummy).toEqual([1, 1, 1]) + expect(dummy).toEqual([1, 1, 1, true]) // nested array mutation state.array[0] = 2 await nextTick() - expect(dummy).toEqual([1, 2, 1]) + expect(dummy).toEqual([1, 2, 1, true]) // nested map mutation state.map.set('a', 2) await nextTick() - expect(dummy).toEqual([1, 2, 2]) + expect(dummy).toEqual([1, 2, 2, true]) + + // nested set mutation + state.set.delete(1) + await nextTick() + expect(dummy).toEqual([1, 2, 2, false]) }) it('lazy', async () => { diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 943011f0..1a956c73 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -164,7 +164,12 @@ function traverse(value: any, seen: Set = new Set()) { for (let i = 0; i < value.length; i++) { traverse(value[i], seen) } - } else if (value instanceof Map || value instanceof Set) { + } else if (value instanceof Map) { + ;(value as any).forEach((v: any, key: any) => { + // to register mutation dep for existing keys + traverse(value.get(key), seen) + }) + } else if (value instanceof Set) { ;(value as any).forEach((v: any) => { traverse(v, seen) })