fix(reactivity): computed should not trigger scheduler if stopped

fix #4149
This commit is contained in:
Evan You 2021-07-19 13:37:03 -04:00
parent dd0f9d1ce6
commit 6eb47f000a
2 changed files with 21 additions and 1 deletions

View File

@ -466,5 +466,25 @@ describe('reactivity/computed', () => {
await tick await tick
expect(effectSpy).toHaveBeenCalledTimes(2) 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)
})
}) })
}) })

View File

@ -58,7 +58,7 @@ class ComputedRefImpl<T> {
scheduled = true scheduled = true
hasCompareTarget = false hasCompareTarget = false
scheduler(() => { scheduler(() => {
if (this._get() !== valueToCompare) { if (this.effect.active && this._get() !== valueToCompare) {
triggerRefValue(this) triggerRefValue(this)
} }
scheduled = false scheduled = false