Skip to content

Commit 99fd158

Browse files
authored
fix(watch): fix deep watchers on refs containing primitives (#984)
1 parent c0adb67 commit 99fd158

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ describe('api: watch', () => {
8686
expect(dummy).toMatchObject([2, 1])
8787
})
8888

89+
it('watching primitive with deep: true', async () => {
90+
const count = ref(0)
91+
let dummy
92+
watch(
93+
count,
94+
(c, prevCount) => {
95+
dummy = [c, prevCount]
96+
},
97+
{
98+
deep: true
99+
}
100+
)
101+
count.value++
102+
await nextTick()
103+
expect(dummy).toMatchObject([1, 0])
104+
})
105+
89106
it('watching multiple sources', async () => {
90107
const state = reactive({ count: 1 })
91108
const count = ref(1)

packages/runtime-core/src/apiWatch.ts

+3
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ export function instanceWatch(
286286

287287
function traverse(value: unknown, seen: Set<unknown> = new Set()) {
288288
if (!isObject(value) || seen.has(value)) {
289+
return value
290+
}
291+
if (seen.has(value)) {
289292
return
290293
}
291294
seen.add(value)

0 commit comments

Comments
 (0)