Skip to content

Commit 818c6bb

Browse files
LinusBorgzhangzhonghe
authored andcommitted
fix(watch): for immediate watch with single source, ensure cb is called with undefined as oldValue (vuejs#7075)
fix: vuejs#7074
1 parent 2308e86 commit 818c6bb

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ describe('api: watch', () => {
745745
const state = ref()
746746
const spy = jest.fn()
747747
watch(() => state.value, spy, { immediate: true })
748-
expect(spy).toHaveBeenCalled()
748+
expect(spy).toHaveBeenCalledWith(undefined, undefined, expect.any(Function))
749749
state.value = 3
750750
await nextTick()
751751
expect(spy).toHaveBeenCalledTimes(2)

packages/runtime-core/src/apiWatch.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,11 @@ function doWatch(
333333
callWithAsyncErrorHandling(cb, instance, ErrorCodes.WATCH_CALLBACK, [
334334
newValue,
335335
// pass undefined as the old value when it's changed for the first time
336-
oldValue === INITIAL_WATCHER_VALUE ||
337-
(isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE)
338-
? []
339-
: oldValue,
336+
oldValue === INITIAL_WATCHER_VALUE
337+
? undefined
338+
: (isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE)
339+
? []
340+
: oldValue,
340341
onCleanup
341342
])
342343
oldValue = newValue

0 commit comments

Comments
 (0)