fix(reactivity): effect should handle self dependency mutations

This commit is contained in:
Evan You 2020-02-18 13:22:09 -05:00
parent e1c9153b9e
commit e8e67729cb
2 changed files with 21 additions and 4 deletions

View File

@ -6,7 +6,8 @@ import {
TrackOpTypes, TrackOpTypes,
TriggerOpTypes, TriggerOpTypes,
DebuggerEvent, DebuggerEvent,
markNonReactive markNonReactive,
ref
} from '../src/index' } from '../src/index'
import { ITERATE_KEY } from '../src/effect' import { ITERATE_KEY } from '../src/effect'
@ -735,4 +736,14 @@ describe('reactivity/effect', () => {
obj.foo = NaN obj.foo = NaN
expect(fnSpy).toHaveBeenCalledTimes(1) 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)
})
}) })

View File

@ -210,11 +210,17 @@ function addRunners(
) { ) {
if (effectsToAdd !== void 0) { if (effectsToAdd !== void 0) {
effectsToAdd.forEach(effect => { effectsToAdd.forEach(effect => {
if (effect !== activeEffect) {
if (effect.options.computed) { if (effect.options.computed) {
computedRunners.add(effect) computedRunners.add(effect)
} else { } else {
effects.add(effect) effects.add(effect)
} }
} else {
// 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
}
}) })
} }
} }