Skip to content

Commit

Permalink
fix(reactivity): computed should not trigger scheduler if stopped
Browse files Browse the repository at this point in the history
fix #4149
  • Loading branch information
yyx990803 committed Jul 19, 2021
1 parent dd0f9d1 commit 6eb47f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
2 changes: 1 addition & 1 deletion packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6eb47f0

Please sign in to comment.