Skip to content

Commit a3e8aaf

Browse files
authoredMay 30, 2024··
fix(watch): support traversing symbol properties in deep watcher (#10969)
close #402
1 parent 6f9587f commit a3e8aaf

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
 

‎packages/runtime-core/__tests__/apiWatch.spec.ts

+46
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,52 @@ describe('api: watch', () => {
932932
expect(dummy).toEqual([1, 2])
933933
})
934934

935+
it('deep with symbols', async () => {
936+
const symbol1 = Symbol()
937+
const symbol2 = Symbol()
938+
const symbol3 = Symbol()
939+
const symbol4 = Symbol()
940+
941+
const raw: any = {
942+
[symbol1]: {
943+
[symbol2]: 1,
944+
},
945+
}
946+
947+
Object.defineProperty(raw, symbol3, {
948+
writable: true,
949+
enumerable: false,
950+
value: 1,
951+
})
952+
953+
const state = reactive(raw)
954+
const spy = vi.fn()
955+
956+
watch(() => state, spy, { deep: true })
957+
958+
await nextTick()
959+
expect(spy).toHaveBeenCalledTimes(0)
960+
961+
state[symbol1][symbol2] = 2
962+
await nextTick()
963+
expect(spy).toHaveBeenCalledTimes(1)
964+
965+
// Non-enumerable properties don't trigger deep watchers
966+
state[symbol3] = 3
967+
await nextTick()
968+
expect(spy).toHaveBeenCalledTimes(1)
969+
970+
// Adding a new symbol property
971+
state[symbol4] = 1
972+
await nextTick()
973+
expect(spy).toHaveBeenCalledTimes(2)
974+
975+
// Removing a symbol property
976+
delete state[symbol4]
977+
await nextTick()
978+
expect(spy).toHaveBeenCalledTimes(3)
979+
})
980+
935981
it('immediate', async () => {
936982
const count = ref(0)
937983
const cb = vi.fn()

‎packages/runtime-core/src/apiWatch.ts

+5
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,11 @@ export function traverse(
493493
for (const key in value) {
494494
traverse(value[key], depth, seen)
495495
}
496+
for (const key of Object.getOwnPropertySymbols(value)) {
497+
if (Object.prototype.propertyIsEnumerable.call(value, key)) {
498+
traverse(value[key as any], depth, seen)
499+
}
500+
}
496501
}
497502
return value
498503
}

0 commit comments

Comments
 (0)
Please sign in to comment.