Skip to content

Commit 785c471

Browse files
committed
fix(reactivity): prevent endless recursion in computed getters
1 parent 6402b98 commit 785c471

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/reactivity/__tests__/computed.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import {
2+
type TestElement,
3+
defineComponent,
24
h,
35
nextTick,
46
nodeOps,
57
onMounted,
68
onUnmounted,
79
render,
810
serializeInner,
11+
triggerEvent,
912
} from '@vue/runtime-test'
1013
import {
1114
type DebuggerEvent,
@@ -944,4 +947,46 @@ describe('reactivity/computed', () => {
944947
newValue: 2,
945948
})
946949
})
950+
951+
test('should prevent endless recursion in self-referencing computed getters', async () => {
952+
const Comp = defineComponent({
953+
data() {
954+
return {
955+
counter: 0,
956+
}
957+
},
958+
959+
computed: {
960+
message(): string {
961+
if (this.counter === 0) {
962+
this.counter++
963+
return this.message
964+
} else {
965+
return `Step ${this.counter}`
966+
}
967+
},
968+
},
969+
970+
render() {
971+
return [
972+
h(
973+
'button',
974+
{
975+
onClick: () => {
976+
this.counter++
977+
},
978+
},
979+
'Step',
980+
),
981+
h('p', this.message),
982+
]
983+
},
984+
})
985+
const root = nodeOps.createElement('div')
986+
render(h(Comp), root)
987+
expect(serializeInner(root)).toBe(`<button>Step</button><p></p>`)
988+
triggerEvent(root.children[1] as TestElement, 'click')
989+
await nextTick()
990+
expect(serializeInner(root)).toBe(`<button>Step</button><p>Step 2</p>`)
991+
})
947992
})

packages/reactivity/src/dep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class Dep {
4646
}
4747

4848
track(debugInfo?: DebuggerEventExtraInfo): Link | undefined {
49-
if (!activeSub || !shouldTrack) {
49+
if (!activeSub || !shouldTrack || activeSub === this.computed) {
5050
return
5151
}
5252

0 commit comments

Comments
 (0)