Skip to content

Commit

Permalink
fix(reactivity): prevent endless recursion in computed getters (#11797)
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni authored Sep 5, 2024
1 parent c74176e commit 716275d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {
type TestElement,
defineComponent,
h,
nextTick,
nodeOps,
onMounted,
onUnmounted,
render,
serializeInner,
triggerEvent,
} from '@vue/runtime-test'
import {
type DebuggerEvent,
Expand Down Expand Up @@ -958,4 +961,46 @@ describe('reactivity/computed', () => {
newValue: 2,
})
})

test('should prevent endless recursion in self-referencing computed getters', async () => {
const Comp = defineComponent({
data() {
return {
counter: 0,
}
},

computed: {
message(): string {
if (this.counter === 0) {
this.counter++
return this.message
} else {
return `Step ${this.counter}`
}
},
},

render() {
return [
h(
'button',
{
onClick: () => {
this.counter++
},
},
'Step',
),
h('p', this.message),
]
},
})
const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(serializeInner(root)).toBe(`<button>Step</button><p></p>`)
triggerEvent(root.children[1] as TestElement, 'click')
await nextTick()
expect(serializeInner(root)).toBe(`<button>Step</button><p>Step 2</p>`)
})
})
2 changes: 1 addition & 1 deletion packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Dep {
}

track(debugInfo?: DebuggerEventExtraInfo): Link | undefined {
if (!activeSub || !shouldTrack) {
if (!activeSub || !shouldTrack || activeSub === this.computed) {
return
}

Expand Down

0 comments on commit 716275d

Please sign in to comment.