Skip to content

Commit f3c5a66

Browse files
fix(overmind): generally fix circular reference classes
1 parent 1fc5ec7 commit f3c5a66

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

packages/node_modules/overmind/src/Devtools.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,10 @@ export type DevtoolsMessage = {
1111
data: any
1212
}
1313

14-
function isClass(value: any) {
15-
return (
16-
typeof value === 'object' &&
17-
!Array.isArray(value) &&
18-
value !== null &&
19-
value.constructor &&
20-
value.constructor.name !== 'Object'
21-
)
22-
}
23-
2414
export class Devtools {
2515
private safeClassNames: Set<string> = new Set()
2616
private unsafeClassNames: Set<string> = new Set()
17+
private circularReferenceCache: any[] = []
2718
private buffer: string[] = []
2819
private ws: WebSocket
2920
private isConnected: boolean = false
@@ -81,6 +72,7 @@ export class Devtools {
8172
send(message: Message) {
8273
const safeClassNames = this.safeClassNames
8374
const unsafeClassNames = this.unsafeClassNames
75+
const circularReferenceCache = this.circularReferenceCache
8476
const stringifiedMessage = JSON.stringify(
8577
message,
8678
function (_, value) {
@@ -100,26 +92,19 @@ export class Devtools {
10092
}
10193
}
10294

103-
if (Array.isArray(value) && isClass(value[0])) {
104-
if (!safeClassNames.has(value[0].constructor.name) && !unsafeClassNames.has(value[0].constructor.name)) {
105-
try {
106-
JSON.stringify(value[0])
107-
safeClassNames.add(value[0].constructor.name)
108-
} catch {
109-
unsafeClassNames.add(value[0].constructor.name)
110-
}
95+
if (
96+
typeof value === 'object' &&
97+
value !== null &&
98+
!Array.isArray(value) &&
99+
value.constructor &&
100+
value.constructor.name !== 'Object'
101+
) {
102+
if (circularReferenceCache.includes(value)) {
103+
return `[CIRCULAR REFERENCE: ${value.constructor.name}]`
111104
}
112-
113-
if (safeClassNames.has(value[0].constructor.name)) {
114-
return value.map((item) => ({
115-
__CLASS__: true,
116-
name: value[0].constructor.name,
117-
value: item
118-
}))
119-
} else {
120-
return value.map(() => `[${value.constructor.name || 'NOT SERIALIZABLE'}]`)
121-
}
122-
} else if (isClass(value)) {
105+
106+
circularReferenceCache.push(value)
107+
123108
if (!safeClassNames.has(value.constructor.name) && !unsafeClassNames.has(value.constructor.name)) {
124109
try {
125110
JSON.stringify(value)
@@ -144,6 +129,7 @@ export class Devtools {
144129
},
145130
0
146131
)
132+
circularReferenceCache.length = 0
147133
this.buffer.push(stringifiedMessage)
148134
this.sendBuffer()
149135
}

0 commit comments

Comments
 (0)