Skip to content

Commit

Permalink
fix(reactivity): fix watch behavior inconsistency + deep ref shallow …
Browse files Browse the repository at this point in the history
…check

fix #12643
  • Loading branch information
yyx990803 committed Jul 12, 2022
1 parent 0825d30 commit 98fb01c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/v3/apiWatch.ts
Expand Up @@ -196,12 +196,10 @@ function doWatch(
getter = () => source.value
forceTrigger = isShallow(source)
} else if (isReactive(source)) {
getter = isArray(source)
? () => {
;(source as any).__ob__.dep.depend()
return source
}
: () => source
getter = () => {
;(source as any).__ob__.dep.depend()
return source
}
deep = true
} else if (isArray(source)) {
isMultiSource = true
Expand Down
2 changes: 1 addition & 1 deletion src/v3/reactivity/ref.ts
Expand Up @@ -68,7 +68,7 @@ function createRef(rawValue: unknown, shallow: boolean) {
}
const ref: any = {}
def(ref, RefFlag, true)
def(ref, ReactiveFlags.IS_SHALLOW, true)
def(ref, ReactiveFlags.IS_SHALLOW, shallow)
def(
ref,
'dep',
Expand Down
17 changes: 17 additions & 0 deletions test/unit/features/v3/apiWatch.spec.ts
Expand Up @@ -1136,4 +1136,21 @@ describe('api: watch', () => {
await nextTick()
expect(spy).toHaveBeenCalledTimes(2)
})

// #12643
test('should trigger watch on reactive object when new property is added via set()', () => {
const spy = vi.fn()
const obj = reactive({})
watch(obj, spy, { flush: 'sync' })
set(obj, 'foo', 1)
expect(spy).toHaveBeenCalled()
})

test('should not trigger watch when calling set() on ref value', () => {
const spy = vi.fn()
const r = ref({})
watch(r, spy, { flush: 'sync' })
set(r.value, 'foo', 1)
expect(spy).not.toHaveBeenCalled()
})
})

0 comments on commit 98fb01c

Please sign in to comment.