Skip to content

Commit

Permalink
fix(watch): for immediate watch with single source, ensure cb is call…
Browse files Browse the repository at this point in the history
…ed with undefined as oldValue (vuejs#7075)

fix: vuejs#7074
  • Loading branch information
LinusBorg authored and zhangzhonghe committed Apr 12, 2023
1 parent 2308e86 commit 818c6bb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ describe('api: watch', () => {
const state = ref()
const spy = jest.fn()
watch(() => state.value, spy, { immediate: true })
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledWith(undefined, undefined, expect.any(Function))
state.value = 3
await nextTick()
expect(spy).toHaveBeenCalledTimes(2)
Expand Down
9 changes: 5 additions & 4 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,11 @@ function doWatch(
callWithAsyncErrorHandling(cb, instance, ErrorCodes.WATCH_CALLBACK, [
newValue,
// pass undefined as the old value when it's changed for the first time
oldValue === INITIAL_WATCHER_VALUE ||
(isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE)
? []
: oldValue,
oldValue === INITIAL_WATCHER_VALUE
? undefined
: (isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE)
? []
: oldValue,
onCleanup
])
oldValue = newValue
Expand Down

0 comments on commit 818c6bb

Please sign in to comment.