Skip to content

Commit

Permalink
fix(reactivity): self-referencing computed should refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 5, 2024
1 parent 716275d commit e84c4a6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
7 changes: 4 additions & 3 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down Expand Up @@ -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',
)
})

Expand Down Expand Up @@ -962,6 +962,7 @@ describe('reactivity/computed', () => {
})
})

// #11797
test('should prevent endless recursion in self-referencing computed getters', async () => {
const Comp = defineComponent({
data() {
Expand Down Expand Up @@ -998,7 +999,7 @@ describe('reactivity/computed', () => {
})
const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(serializeInner(root)).toBe(`<button>Step</button><p></p>`)
expect(serializeInner(root)).toBe(`<button>Step</button><p>Step 1</p>`)
triggerEvent(root.children[1] as TestElement, 'click')
await nextTick()
expect(serializeInner(root)).toBe(`<button>Step</button><p>Step 2</p>`)
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ export class ComputedRefImpl<T = any> 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
Expand Down
3 changes: 0 additions & 3 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ function isDirty(sub: Subscriber): boolean {
* @internal
*/
export function refreshComputed(computed: ComputedRefImpl): false | undefined {
if (computed.flags & EffectFlags.RUNNING) {
return false
}
if (
computed.flags & EffectFlags.TRACKING &&
!(computed.flags & EffectFlags.DIRTY)
Expand Down

0 comments on commit e84c4a6

Please sign in to comment.