Skip to content

Commit

Permalink
fix(watch): traverse refs in deep watch (#753)
Browse files Browse the repository at this point in the history
Co-authored-by: webfansplz <>
  • Loading branch information
webfansplz committed Jul 12, 2021
1 parent bc7c2af commit 55a0a20
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/apis/watch.ts
Expand Up @@ -332,6 +332,11 @@ function createWatcher(
)
}

if (deep) {
const baseGetter = getter
getter = () => traverse(baseGetter())
}

const applyCb = (n: any, o: any) => {
// cleanup before running cb again
runCleanup()
Expand Down
19 changes: 19 additions & 0 deletions test/v3/runtime-core/apiWatch.spec.ts
Expand Up @@ -531,4 +531,23 @@ describe('api: watch', () => {

expect(data2.value).toMatchObject([1])
})

it('watching deep ref', async () => {
const count = ref(0)
const double = computed(() => count.value * 2)
const state = reactive([count, double])

let dummy
watch(
() => state,
(state) => {
dummy = [state[0].value, state[1].value]
},
{ deep: true }
)

count.value++
await nextTick()
expect(dummy).toEqual([1, 2])
})
})

0 comments on commit 55a0a20

Please sign in to comment.