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(runtime-core): trigger watcher with undefined as initial value #687

Merged
merged 1 commit into from
Feb 4, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ describe('api: watch', () => {
expect(dummy).toBe(1)
})

it('triggers when initial value is null', async () => {
const state = ref(null)
const spy = jest.fn()
watch(() => state.value, spy)
await nextTick()
expect(spy).toHaveBeenCalled()
})

it('triggers when initial value is undefined', async () => {
const state = ref()
const spy = jest.fn()
watch(() => state.value, spy)
await nextTick()
expect(spy).toHaveBeenCalled()
state.value = 3
await nextTick()
expect(spy).toHaveBeenCalledTimes(2)
// testing if undefined can trigger the watcher
state.value = undefined
await nextTick()
expect(spy).toHaveBeenCalledTimes(3)
// it shouldn't trigger if the same value is set
state.value = undefined
await nextTick()
expect(spy).toHaveBeenCalledTimes(3)
})

it('watching single source: getter', async () => {
const state = reactive({ count: 0 })
let dummy
Expand Down
8 changes: 6 additions & 2 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export type StopHandle = () => void

const invoke = (fn: Function) => fn()

// initial value for watchers to trigger on undefined initial values
const INITIAL_WATCHER_VALUE = {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use a Symbol but it shouldn't change anything since this variable is never exposed to user code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, since objects are unique your way is good too. But symbols can help e.g. in debug of Vue itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But symbols can help e.g. in debug of Vue itself.

What case are you thinking of?

Symbols are also not supported by all platforms (even though we are using them in quite a few places already). To me, using an object works everywhere while being as simple as using Symbols and I couldn't think of a case where it would fail

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, object wins!


// overload #1: simple effect
export function watch(effect: WatchEffect, options?: WatchOptions): StopHandle

Expand Down Expand Up @@ -153,7 +156,7 @@ function doWatch(
}
}

let oldValue = isArray(source) ? [] : undefined
let oldValue = isArray(source) ? [] : INITIAL_WATCHER_VALUE
const applyCb = cb
? () => {
if (instance && instance.isUnmounted) {
Expand All @@ -167,7 +170,8 @@ function doWatch(
}
callWithAsyncErrorHandling(cb, instance, ErrorCodes.WATCH_CALLBACK, [
newValue,
oldValue,
// pass undefined as the old value when it's changed for the first time
oldValue === INITIAL_WATCHER_VALUE ? undefined : oldValue,
registerCleanup
])
oldValue = newValue
Expand Down