Skip to content

Commit 98fb01c

Browse files
committed
fix(reactivity): fix watch behavior inconsistency + deep ref shallow check
fix #12643
1 parent 0825d30 commit 98fb01c

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

Diff for: src/v3/apiWatch.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,10 @@ function doWatch(
196196
getter = () => source.value
197197
forceTrigger = isShallow(source)
198198
} else if (isReactive(source)) {
199-
getter = isArray(source)
200-
? () => {
201-
;(source as any).__ob__.dep.depend()
202-
return source
203-
}
204-
: () => source
199+
getter = () => {
200+
;(source as any).__ob__.dep.depend()
201+
return source
202+
}
205203
deep = true
206204
} else if (isArray(source)) {
207205
isMultiSource = true

Diff for: src/v3/reactivity/ref.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function createRef(rawValue: unknown, shallow: boolean) {
6868
}
6969
const ref: any = {}
7070
def(ref, RefFlag, true)
71-
def(ref, ReactiveFlags.IS_SHALLOW, true)
71+
def(ref, ReactiveFlags.IS_SHALLOW, shallow)
7272
def(
7373
ref,
7474
'dep',

Diff for: test/unit/features/v3/apiWatch.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1136,4 +1136,21 @@ describe('api: watch', () => {
11361136
await nextTick()
11371137
expect(spy).toHaveBeenCalledTimes(2)
11381138
})
1139+
1140+
// #12643
1141+
test('should trigger watch on reactive object when new property is added via set()', () => {
1142+
const spy = vi.fn()
1143+
const obj = reactive({})
1144+
watch(obj, spy, { flush: 'sync' })
1145+
set(obj, 'foo', 1)
1146+
expect(spy).toHaveBeenCalled()
1147+
})
1148+
1149+
test('should not trigger watch when calling set() on ref value', () => {
1150+
const spy = vi.fn()
1151+
const r = ref({})
1152+
watch(r, spy, { flush: 'sync' })
1153+
set(r.value, 'foo', 1)
1154+
expect(spy).not.toHaveBeenCalled()
1155+
})
11391156
})

0 commit comments

Comments
 (0)