]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(watch): fix watching reactive array (#1656)
authorTan Li Hau <tanhauhau@users.noreply.github.com>
Mon, 20 Jul 2020 16:39:22 +0000 (00:39 +0800)
committerGitHub <noreply@github.com>
Mon, 20 Jul 2020 16:39:22 +0000 (12:39 -0400)
fixes #1655

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

index 07b985c3c08c6ca2f79404c617a1e42dfa7e662b..331b6f3c30558a66e5e8ea0546c4309d71d46135 100644 (file)
@@ -69,6 +69,16 @@ describe('api: watch', () => {
     expect(dummy).toMatchObject([1, 0])
   })
 
+  it('watching single source: array', async () => {
+    const array = reactive([] as number[])
+    const spy = jest.fn()
+    watch(array, spy)
+    array.push(1)
+    await nextTick()
+    expect(spy).toBeCalledTimes(1)
+    expect(spy).toBeCalledWith([1], expect.anything(), expect.anything())
+  })
+
   it('watching single source: computed ref', async () => {
     const count = ref(0)
     const plus = computed(() => count.value + 1)
index fcc8c3203089cbbbfd157ad58f3c63b89d753499..de6f54e9e9bdaf98b8c298d227d5939349c830e4 100644 (file)
@@ -159,7 +159,12 @@ function doWatch(
   }
 
   let getter: () => any
-  if (isArray(source)) {
+  if (isRef(source)) {
+    getter = () => source.value
+  } else if (isReactive(source)) {
+    getter = () => source
+    deep = true
+  } else if (isArray(source)) {
     getter = () =>
       source.map(s => {
         if (isRef(s)) {
@@ -172,11 +177,6 @@ function doWatch(
           __DEV__ && warnInvalidSource(s)
         }
       })
-  } else if (isRef(source)) {
-    getter = () => source.value
-  } else if (isReactive(source)) {
-    getter = () => source
-    deep = true
   } else if (isFunction(source)) {
     if (cb) {
       // getter with cb