Skip to content

Commit 6eb47f0

Browse files
committed
fix(reactivity): computed should not trigger scheduler if stopped
fix #4149
1 parent dd0f9d1 commit 6eb47f0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/reactivity/__tests__/computed.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -466,5 +466,25 @@ describe('reactivity/computed', () => {
466466
await tick
467467
expect(effectSpy).toHaveBeenCalledTimes(2)
468468
})
469+
470+
test('should not compute if deactivated before scheduler is called', async () => {
471+
const c1Spy = jest.fn()
472+
const src = ref(0)
473+
const c1 = computed(() => {
474+
c1Spy()
475+
return src.value % 2
476+
})
477+
effect(() => c1.value)
478+
expect(c1Spy).toHaveBeenCalledTimes(1)
479+
480+
// schedule stop
481+
schedule(() => {
482+
c1.effect.stop()
483+
})
484+
// trigger
485+
src.value++
486+
await tick
487+
expect(c1Spy).toHaveBeenCalledTimes(1)
488+
})
469489
})
470490
})

packages/reactivity/src/computed.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ComputedRefImpl<T> {
5858
scheduled = true
5959
hasCompareTarget = false
6060
scheduler(() => {
61-
if (this._get() !== valueToCompare) {
61+
if (this.effect.active && this._get() !== valueToCompare) {
6262
triggerRefValue(this)
6363
}
6464
scheduled = false

0 commit comments

Comments
 (0)