Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(watch): traverse refs in deep watch #753

Merged
merged 1 commit into from Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/apis/watch.ts
Expand Up @@ -317,6 +317,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])
})
})