From e596378e0be728dad7d60938449f3fa557ca2ec9 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 10 Sep 2024 15:55:41 +0800 Subject: [PATCH] fix: Revert "fix: Revert "fix(reactivity): self-referencing computed should refresh"" This reverts commit 35c760f82f749f7c6e3f9bfead8221ce498e892f. --- packages/reactivity/__tests__/computed.spec.ts | 7 ++++--- packages/reactivity/src/computed.ts | 2 +- packages/reactivity/src/effect.ts | 7 ++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/reactivity/__tests__/computed.spec.ts b/packages/reactivity/__tests__/computed.spec.ts index 31daef559a8..e0b47cf56eb 100644 --- a/packages/reactivity/__tests__/computed.spec.ts +++ b/packages/reactivity/__tests__/computed.spec.ts @@ -594,7 +594,7 @@ describe('reactivity/computed', () => { v.value += ' World' await nextTick() - expect(serializeInner(root)).toBe('Hello World World World') + expect(serializeInner(root)).toBe('Hello World World World World') // expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned() }) @@ -892,7 +892,7 @@ describe('reactivity/computed', () => { v.value += ' World' await nextTick() expect(serializeInner(root)).toBe( - 'Hello World World World | Hello World World World', + 'Hello World World World World | Hello World World World World', ) }) @@ -962,6 +962,7 @@ describe('reactivity/computed', () => { }) }) + // #11797 test('should prevent endless recursion in self-referencing computed getters', async () => { const Comp = defineComponent({ data() { @@ -998,7 +999,7 @@ describe('reactivity/computed', () => { }) const root = nodeOps.createElement('div') render(h(Comp), root) - expect(serializeInner(root)).toBe(`

`) + expect(serializeInner(root)).toBe(`

Step 1

`) triggerEvent(root.children[1] as TestElement, 'click') await nextTick() expect(serializeInner(root)).toBe(`

Step 2

`) diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index aa5d2079061..d2dd67bf97c 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -111,9 +111,9 @@ export class ComputedRefImpl implements Subscriber { * @internal */ notify(): void { + this.flags |= EffectFlags.DIRTY // avoid infinite self recursion if (activeSub !== this) { - this.flags |= EffectFlags.DIRTY this.dep.notify() } else if (__DEV__) { // TODO warn diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index 88493e4e9a9..678ee1982fe 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -326,7 +326,7 @@ function isDirty(sub: Subscriber): boolean { for (let link = sub.deps; link; link = link.nextDep) { if ( link.dep.version !== link.version || - (link.dep.computed && refreshComputed(link.dep.computed) === false) || + (link.dep.computed && refreshComputed(link.dep.computed)) || link.dep.version !== link.version ) { return true @@ -344,10 +344,7 @@ function isDirty(sub: Subscriber): boolean { * Returning false indicates the refresh failed * @internal */ -export function refreshComputed(computed: ComputedRefImpl): false | undefined { - if (computed.flags & EffectFlags.RUNNING) { - return false - } +export function refreshComputed(computed: ComputedRefImpl): undefined { if ( computed.flags & EffectFlags.TRACKING && !(computed.flags & EffectFlags.DIRTY)