Quellcode durchsuchen

fix(watch): trigger immediate callback for empty sources (#14914)

close #14898
Puneet Dixit vor 5 Tagen
Ursprung
Commit
1f2ca7e483

+ 1 - 0
packages/reactivity/src/watch.ts

@@ -242,6 +242,7 @@ export function watch(
       // watch(source, cb)
       const newValue = effect.run()
       if (
+        immediateFirstRun ||
         deep ||
         forceTrigger ||
         (isMultiSource

+ 7 - 0
packages/runtime-core/__tests__/apiWatch.spec.ts

@@ -312,6 +312,13 @@ describe('api: watch', () => {
     expect(called).toBe(true)
   })
 
+  it('watching empty multiple sources with immediate: true', () => {
+    const spy = vi.fn()
+    watch([], spy, { immediate: true })
+    expect(spy).toHaveBeenCalledTimes(1)
+    expect(spy).toHaveBeenCalledWith([], [], expect.any(Function))
+  })
+
   it('watching multiple sources: readonly array', async () => {
     const state = reactive({ count: 1 })
     const status = ref(false)