]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): watching multiple values - handle `undefined` as initial values...
authorThorsten Lünborg <t.luenborg@googlemail.com>
Thu, 20 Oct 2022 19:45:05 +0000 (21:45 +0200)
committerGitHub <noreply@github.com>
Thu, 20 Oct 2022 19:45:05 +0000 (21:45 +0200)
packages/runtime-core/__tests__/apiWatch.spec.ts
packages/runtime-core/src/apiWatch.ts

index 86ad948adf628a6e9d6b42b8153c8c91d17b6c56..c9db3657367beac6816d07d8a24d3ae8a719f774 100644 (file)
@@ -177,6 +177,23 @@ describe('api: watch', () => {
     ])
   })
 
+  it('watching multiple sources: undefined initial values and immediate: true', async () => {
+    const a = ref()
+    const b = ref()
+    let called = false
+    watch(
+      [a, b],
+      (newVal, oldVal) => {
+        called = true
+        expect(newVal).toMatchObject([undefined, undefined])
+        expect(oldVal).toBeUndefined()
+      },
+      { immediate: true }
+    )
+    await nextTick()
+    expect(called).toBe(true)
+  })
+
   it('watching multiple sources: readonly array', async () => {
     const state = reactive({ count: 1 })
     const status = ref(false)
index 27215f342b38dfeddba8cc14656dbf8f9e838b1b..19026cf645dff3d856db57c602ed0b25aece9acd 100644 (file)
@@ -296,7 +296,9 @@ function doWatch(
     return NOOP
   }
 
-  let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE
+  let oldValue: any = isMultiSource
+    ? new Array((source as []).length).fill(INITIAL_WATCHER_VALUE)
+    : INITIAL_WATCHER_VALUE
   const job: SchedulerJob = () => {
     if (!effect.active) {
       return
@@ -323,7 +325,10 @@ 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 ? undefined : oldValue,
+          oldValue === INITIAL_WATCHER_VALUE ||
+          (isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE)
+            ? undefined
+            : oldValue,
           onCleanup
         ])
         oldValue = newValue