Skip to content

Commit

Permalink
fix: trigger when adding new property with Vue.seta and piWatch test
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowei-one committed Feb 28, 2023
1 parent a9ca2d8 commit 4d51702
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/v3/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,17 @@ function doWatch(
})
} else if (isFunction(source)) {
if (cb) {
// getter with cb
getter = () => call(source, WATCHER_GETTER)
const ob = call(source, WATCHER_GETTER)
if (isReactive(ob)){
getter = () => {
;(ob as any).__ob__.dep.depend()
return ob
}
deep = true
} else {
// getter with cb
getter = () => ob
}
} else {
// no cb -> simple effect
getter = () => {
Expand Down
11 changes: 11 additions & 0 deletions test/unit/features/v3/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,4 +1200,15 @@ describe('api: watch', () => {
expect(parentSpy).toHaveBeenCalledTimes(1)
expect(childSpy).toHaveBeenCalledTimes(1)
})

// #12967
test('trigger when adding new property with Vue.set', async () => {
const spy = vi.fn()
const r = reactive({ exist: 5 })
watch(() => r, spy, { deep: true })
set(r, 'add', 1)

await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})
})

0 comments on commit 4d51702

Please sign in to comment.