Skip to content

Commit 1a2c3c2

Browse files
committed
fix(watch): fix deep watch for structures containing raw refs
fix #12652
1 parent 005e52d commit 1a2c3c2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/core/observer/traverse.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { _Set as Set, isObject, isArray } from '../util/index'
22
import type { SimpleSet } from '../util/index'
33
import VNode from '../vdom/vnode'
4+
import { isRef } from '../../v3'
45

56
const seenObjects = new Set()
67

@@ -35,6 +36,8 @@ function _traverse(val: any, seen: SimpleSet) {
3536
if (isA) {
3637
i = val.length
3738
while (i--) _traverse(val[i], seen)
39+
} else if (isRef(val)) {
40+
_traverse(val.value, seen)
3841
} else {
3942
keys = Object.keys(val)
4043
i = keys.length

test/unit/features/v3/apiWatch.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ describe('api: watch', () => {
144144
expect(dummy).toBe(1)
145145
})
146146

147+
it('deep watch w/ raw refs', async () => {
148+
const count = ref(0)
149+
const src = reactive({
150+
arr: [count]
151+
})
152+
let dummy
153+
watch(src, ({ arr: [{ value }] }) => {
154+
dummy = value
155+
})
156+
count.value++
157+
await nextTick()
158+
expect(dummy).toBe(1)
159+
})
160+
147161
it('watching multiple sources', async () => {
148162
const state = reactive({ count: 1 })
149163
const count = ref(1)

0 commit comments

Comments
 (0)