Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(watch): deep watching symbol properties #10969

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,52 @@ describe('api: watch', () => {
expect(dummy).toEqual([1, 2])
})

it('deep with symbols', async () => {
const symbol1 = Symbol()
const symbol2 = Symbol()
const symbol3 = Symbol()
const symbol4 = Symbol()

const raw: any = {
[symbol1]: {
[symbol2]: 1,
},
}

Object.defineProperty(raw, symbol3, {
writable: true,
enumerable: false,
value: 1,
})

const state = reactive(raw)
const spy = vi.fn()

watch(() => state, spy, { deep: true })

await nextTick()
expect(spy).toHaveBeenCalledTimes(0)

state[symbol1][symbol2] = 2
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)

// Non-enumerable properties don't trigger deep watchers
state[symbol3] = 3
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)

// Adding a new symbol property
state[symbol4] = 1
await nextTick()
expect(spy).toHaveBeenCalledTimes(2)

// Removing a symbol property
delete state[symbol4]
await nextTick()
expect(spy).toHaveBeenCalledTimes(3)
})

it('immediate', async () => {
const count = ref(0)
const cb = vi.fn()
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ export function traverse(
for (const key in value) {
traverse(value[key], depth, seen)
}
for (const key of Object.getOwnPropertySymbols(value)) {
if (Object.prototype.propertyIsEnumerable.call(value, key)) {
traverse(value[key as any], depth, seen)
}
}
}
return value
}