Skip to content

Commit 2bd6ea5

Browse files
authored
fix(customReactive): avoid circular reference. (#758)
1 parent 55a0a20 commit 2bd6ea5

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/mixin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ export function mixin(Vue: VueConstructor) {
149149
}
150150
}
151151

152-
function customReactive(target: object) {
152+
function customReactive(target: object, visited = new Set()) {
153+
if (visited.has(target)) return
153154
if (
154155
!isPlainObject(target) ||
155156
isRef(target) ||
@@ -165,7 +166,8 @@ export function mixin(Vue: VueConstructor) {
165166
const val = target[k]
166167
defineReactive(target, k, val)
167168
if (val) {
168-
customReactive(val)
169+
visited.add(val)
170+
customReactive(val, visited)
169171
}
170172
return
171173
})

test/setupContext.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import {
55
ref,
66
nextTick,
77
SetupContext,
8+
getCurrentInstance,
89
} from '../src'
10+
import { mockWarn } from './helpers'
911

1012
describe('setupContext', () => {
13+
mockWarn(true)
1114
it('should have proper properties', () => {
1215
let context: SetupContext = undefined!
1316

@@ -195,4 +198,31 @@ describe('setupContext', () => {
195198

196199
expect(_attrs.foo).toBe('bar2')
197200
})
201+
202+
// #563
203+
it('should not RangeError: Maximum call stack size exceeded', async () => {
204+
createApp(
205+
defineComponent({
206+
template: `<div/>`,
207+
setup() {
208+
// @ts-expect-error
209+
const app = getCurrentInstance().proxy
210+
let mockNT: any = []
211+
mockNT.__ob__ = {}
212+
const test = {
213+
app,
214+
mockNT,
215+
}
216+
return {
217+
test,
218+
}
219+
},
220+
})
221+
).mount()
222+
223+
await nextTick()
224+
expect(
225+
`"RangeError: Maximum call stack size exceeded"`
226+
).not.toHaveBeenWarned()
227+
})
198228
})

0 commit comments

Comments
 (0)