Skip to content

Commit 91240cf

Browse files
fix(overmind): use a proxy-state-tree stringifier
1 parent 8674dac commit 91240cf

File tree

6 files changed

+69
-35
lines changed

6 files changed

+69
-35
lines changed

packages/node_modules/overmind-devtools/bin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const serverPath = path.join(__dirname, 'src/main.js')
88

99
const args = [serverPath]
1010
.concat([].concat(process.argv).splice(2))
11-
// .concat('--not-packaged=true')
11+
.concat('--not-packaged=true')
1212

1313
const env = Object.create(process.env)
1414
env.NODE_ENV = 'production'

packages/node_modules/overmind/src/Devtools.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { stringify } from 'proxy-state-tree'
2+
13
export type Message = {
24
type: string
35
data?: object
@@ -69,42 +71,53 @@ export class Devtools {
6971
send(message: Message) {
7072
const safeClassNames = this.safeClassNames
7173
const unsafeClassNames = this.unsafeClassNames
72-
const stringifiedMessage = JSON.stringify(message, function (_, value) {
73-
if (typeof value === 'function') {
74-
return '[Function]'
75-
}
76-
if (this.__CLASS__) {
77-
return value
78-
}
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-
}
74+
const stringifiedMessage = stringify(
75+
message,
76+
function (_, value) {
77+
if (typeof value === 'function') {
78+
return '[Function]'
9379
}
94-
95-
if (safeClassNames.has(value.constructor.name)) {
96-
return {
97-
__CLASS__: true,
98-
name: value.constructor.name,
99-
value
80+
if (this.__CLASS__) {
81+
return value
82+
}
83+
if (
84+
typeof value === 'object' &&
85+
value !== null &&
86+
!Array.isArray(value) &&
87+
value.constructor &&
88+
value.constructor.name !== 'Object'
89+
) {
90+
if (!safeClassNames.has(value.constructor.name) && !unsafeClassNames.has(value.constructor.name)) {
91+
try {
92+
JSON.stringify(value)
93+
safeClassNames.add(value.constructor.name)
94+
} catch {
95+
unsafeClassNames.add(value.constructor.name)
96+
}
97+
}
98+
99+
if (safeClassNames.has(value.constructor.name)) {
100+
return {
101+
__CLASS__: true,
102+
name: value.constructor.name,
103+
value
104+
}
105+
} else {
106+
return `[${value.constructor.name || 'NOT SERIALIZABLE'}]`
100107
}
101-
} else {
102-
return `[${value.constructor.name || 'NOT SERIALIZABLE'}]`
103108
}
104-
}
105109

106-
return value
107-
})
110+
return value
111+
},
112+
0,
113+
function (value) {
114+
return {
115+
__CLASS__: true,
116+
name: this.constructor.name,
117+
value
118+
}
119+
}
120+
)
108121
this.buffer.push(stringifiedMessage)
109122
this.sendBuffer()
110123
}

packages/node_modules/overmind/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ export class Overmind<ThisConfig extends IConfiguration>
833833
devtools.send({
834834
type: 'init',
835835
data: {
836-
state: this.proxyStateTree.state[VALUE],
836+
state: this.proxyStateTree.state,
837837
actions: getActionPaths(actions),
838838
},
839839
})

packages/node_modules/proxy-state-tree/src/Proxyfier.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ export const PATH = Symbol('PATH')
77
export const VALUE = Symbol('VALUE')
88
export const PROXY_TREE = Symbol('PROXY_TREE')
99

10+
let JSONReplacer = null
11+
12+
export const stringify = (
13+
value: any,
14+
replacer?: (this: any, key: string, value: any) => any,
15+
space?: string | number,
16+
toJSONReplacer?: (value: any) => any
17+
) => {
18+
JSONReplacer = toJSONReplacer
19+
const result = JSON.stringify(value, replacer, space)
20+
JSONReplacer = null
21+
return result
22+
}
23+
1024
const arrayMutations = new Set([
1125
'push',
1226
'shift',
@@ -298,7 +312,7 @@ export class Proxifier {
298312
const currentTree = trackingTree || proxifier.tree
299313

300314
if (typeof targetValue === 'function' && isClass(target)) {
301-
return (...args) => targetValue.call(proxy, ...args)
315+
return (...args) => prop === 'toJSON' && JSONReplacer ? JSONReplacer.call(proxy, targetValue.call(proxy, ...args)) : targetValue.call(proxy, ...args)
302316
} else if (typeof targetValue === 'function') {
303317
return proxifier.tree.master.options.dynamicWrapper
304318
? proxifier.tree.master.options.dynamicWrapper(

packages/node_modules/proxy-state-tree/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export {
2929
MutationTree,
3030
}
3131

32+
export { stringify } from './Proxyfier'
33+
3234
export * from './types'
3335

3436
export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {

packages/overmind-website/src/overmind/state.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type State = {
3838

3939
class User {
4040
name = 'Bob'
41+
toJSON() {
42+
return {
43+
name: 'ARNE',
44+
}
45+
}
4146
}
4247

4348
const state: State = {

0 commit comments

Comments
 (0)