Skip to content

Commit aaab190

Browse files
feat(overmind): optimize stringifying classes
1 parent d23697d commit aaab190

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

packages/node_modules/overmind/src/Devtools.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export type DevtoolsMessage = {
1010
}
1111

1212
export class Devtools {
13+
private safeClassNames: Set<string> = new Set()
14+
private unsafeClassNames: Set<string> = new Set()
1315
private buffer: string[] = []
1416
private ws: WebSocket
1517
private isConnected: boolean = false
@@ -65,24 +67,39 @@ export class Devtools {
6567
)
6668
}
6769
send(message: Message) {
70+
const safeClassNames = this.safeClassNames
71+
const unsafeClassNames = this.unsafeClassNames
6872
const stringifiedMessage = JSON.stringify(message, function (_, value) {
6973
if (typeof value === 'function') {
7074
return '[Function]'
7175
}
7276
if (this.__CLASS__) {
7377
return value
7478
}
75-
if (typeof value === 'object' && value !== null && !Array.isArray(value) && value.constructor && value.constructor.name !== 'Object') {
76-
try {
77-
JSON.stringify(value)
78-
} catch {
79-
return `[${value.constructor.name || 'NOT SERIALIZABLE'}]`
79+
if (
80+
typeof value === 'object' &&
81+
value !== null &&
82+
!Array.isArray(value) &&
83+
value.constructor &&
84+
value.constructor.name !== 'Object'
85+
) {
86+
if (!safeClassNames.has(value.constructor.name) && !unsafeClassNames.has(value.constructor.name)) {
87+
try {
88+
JSON.stringify(value)
89+
safeClassNames.add(value.constructor.name)
90+
} catch {
91+
unsafeClassNames.add(value.constructor.name)
92+
}
8093
}
81-
82-
return {
83-
__CLASS__: true,
84-
name: value.constructor.name,
85-
value
94+
95+
if (safeClassNames.has(value.constructor.name)) {
96+
return {
97+
__CLASS__: true,
98+
name: value.constructor.name,
99+
value
100+
}
101+
} else {
102+
return `[${value.constructor.name || 'NOT SERIALIZABLE'}]`
86103
}
87104
}
88105

0 commit comments

Comments
 (0)