]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): computed should not trigger scheduler if stopped
authorEvan You <yyx990803@gmail.com>
Mon, 19 Jul 2021 17:37:03 +0000 (13:37 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 19 Jul 2021 17:37:03 +0000 (13:37 -0400)
fix #4149

packages/reactivity/__tests__/computed.spec.ts
packages/reactivity/src/computed.ts

index 3f40b1c45ec634e2aeaa966793611990130a9138..34aec01d3e9e07fd1ab73d921c821e92d6362067 100644 (file)
@@ -466,5 +466,25 @@ describe('reactivity/computed', () => {
       await tick
       expect(effectSpy).toHaveBeenCalledTimes(2)
     })
+
+    test('should not compute if deactivated before scheduler is called', async () => {
+      const c1Spy = jest.fn()
+      const src = ref(0)
+      const c1 = computed(() => {
+        c1Spy()
+        return src.value % 2
+      })
+      effect(() => c1.value)
+      expect(c1Spy).toHaveBeenCalledTimes(1)
+
+      // schedule stop
+      schedule(() => {
+        c1.effect.stop()
+      })
+      // trigger
+      src.value++
+      await tick
+      expect(c1Spy).toHaveBeenCalledTimes(1)
+    })
   })
 })
index c992031d5ffce77df93aa6d4d91856e33affd9ab..fdc5c58f8082bebdbbe3a586f256fa8ad0eaa5a4 100644 (file)
@@ -58,7 +58,7 @@ class ComputedRefImpl<T> {
           scheduled = true
           hasCompareTarget = false
           scheduler(() => {
-            if (this._get() !== valueToCompare) {
+            if (this.effect.active && this._get() !== valueToCompare) {
               triggerRefValue(this)
             }
             scheduled = false