Skip to content

Commit

Permalink
fix(reactivity): effect should handle self dependency mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 18, 2020
1 parent e1c9153 commit e8e6772
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
13 changes: 12 additions & 1 deletion packages/reactivity/__tests__/effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
TrackOpTypes,
TriggerOpTypes,
DebuggerEvent,
markNonReactive
markNonReactive,
ref
} from '../src/index'
import { ITERATE_KEY } from '../src/effect'

Expand Down Expand Up @@ -735,4 +736,14 @@ describe('reactivity/effect', () => {
obj.foo = NaN
expect(fnSpy).toHaveBeenCalledTimes(1)
})

it('should handle self dependency mutations', () => {
const count = ref(0)
effect(() => {
count.value++
})
expect(count.value).toBe(1)
count.value = 10
expect(count.value).toBe(11)
})
})
12 changes: 9 additions & 3 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ function addRunners(
) {
if (effectsToAdd !== void 0) {
effectsToAdd.forEach(effect => {
if (effect.options.computed) {
computedRunners.add(effect)
if (effect !== activeEffect) {
if (effect.options.computed) {
computedRunners.add(effect)
} else {
effects.add(effect)
}
} else {
effects.add(effect)
// the effect mutated its own dependency during its execution.
// this can be caused by operations like foo.value++
// do not trigger or we end in an infinite loop
}
})
}
Expand Down

0 comments on commit e8e6772

Please sign in to comment.