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): fire callback when original value is undefined #688

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,13 @@ describe('api: watch', () => {
oldValue: 2
})
})

// #683
it('watching undefined', async () => {
let dummy = null
const r = ref(undefined)
watch(r, v => (dummy = v))
await nextTick()
expect(dummy).toBe(undefined)
})
})
6 changes: 5 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,18 @@ function doWatch(
}
}

let oldValue = isArray(source) ? [] : undefined
const watchDefaultSymbol = (Symbol('watchDefault') as unknown) as undefined // Unique symbol for default, see #683
let oldValue = isArray(source) ? [] : watchDefaultSymbol
const applyCb = cb
? () => {
if (instance && instance.isUnmounted) {
return
}
const newValue = runner()
if (deep || hasChanged(newValue, oldValue)) {
if (oldValue === watchDefaultSymbol) {
oldValue = undefined
}
// cleanup before running cb again
if (cleanup) {
cleanup()
Expand Down