Skip to content

Commit 1f2ca7e

Browse files
fix(watch): trigger immediate callback for empty sources (#14914)
close #14898
1 parent 450a8a8 commit 1f2ca7e

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

‎packages/reactivity/src/watch.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ export function watch(
242242
// watch(source, cb)
243243
const newValue = effect.run()
244244
if (
245+
immediateFirstRun ||
245246
deep ||
246247
forceTrigger ||
247248
(isMultiSource

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ describe('api: watch', () => {
312312
expect(called).toBe(true)
313313
})
314314

315+
it('watching empty multiple sources with immediate: true', () => {
316+
const spy = vi.fn()
317+
watch([], spy, { immediate: true })
318+
expect(spy).toHaveBeenCalledTimes(1)
319+
expect(spy).toHaveBeenCalledWith([], [], expect.any(Function))
320+
})
321+
315322
it('watching multiple sources: readonly array', async () => {
316323
const state = reactive({ count: 1 })
317324
const status = ref(false)

0 commit comments

Comments
 (0)