Browse Source

fix(watch): ensure oldValue in multi-source watcher is always an array

fix #7070
Evan You 3 years ago
parent
commit
23e85e21a5

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

@@ -183,10 +183,10 @@ describe('api: watch', () => {
     let called = false
     watch(
       [a, b],
-      (newVal, oldVal) => {
+      ([newA, newB], [oldA, oldB]) => {
         called = true
-        expect(newVal).toMatchObject([undefined, undefined])
-        expect(oldVal).toBeUndefined()
+        expect([newA, newB]).toMatchObject([undefined, undefined])
+        expect([oldA, oldB]).toMatchObject([undefined, undefined])
       },
       { immediate: true }
     )

+ 1 - 1
packages/runtime-core/src/apiWatch.ts

@@ -335,7 +335,7 @@ function doWatch(
           // pass undefined as the old value when it's changed for the first time
           oldValue === INITIAL_WATCHER_VALUE ||
           (isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE)
-            ? undefined
+            ? []
             : oldValue,
           onCleanup
         ])