Skip to content

fix(reactivity): mark the effect as recursive if there's updates during the run #10090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ describe('reactivity/computed', () => {
expect(c1.value).toBe(1)
})

it('should work when chained(ref+computed)', () => {
const value = ref(0)
const provider = computed(() => value.value + consumer.value)
const consumer = computed(() => {
value.value++
return 'foo'
})
expect(provider.value).toBe('1foo')

value.value += 1
expect(provider.value).toBe('3foo')
value.value += 1
expect(provider.value).toBe('5foo')
})

it('should trigger effect when chained', () => {
const value = reactive({ foo: 0 })
const getter1 = vi.fn(() => value.foo)
Expand Down
17 changes: 15 additions & 2 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ export class ReactiveEffect<T = any> {
activeEffect = this
this._runnings++
preCleanupEffect(this)
return this.fn()
const r = this.fn()
// @ts-expect-error this has become dirty in the run
if (this._dirtyLevel === DirtyLevels.Dirty) {
return this.fn()
}
return r
} finally {
postCleanupEffect(this)
this._runnings--
Expand Down Expand Up @@ -289,9 +294,17 @@ export function triggerEffects(
debuggerEventExtraInfo?: DebuggerEventExtraInfo,
) {
pauseScheduling()
const hasDepsRunning = activeEffect ? dep.has(activeEffect) : false
for (const effect of dep.keys()) {
if (!effect.allowRecurse && effect._runnings) {
continue
if (hasDepsRunning && effect !== activeEffect) {
// maybe this effect is actually recursive
if (effect.allowRecurse === undefined) {
effect.allowRecurse = true
}
} else {
continue
}
}
if (
effect._dirtyLevel < dirtyLevel &&
Expand Down
Loading