Skip to content

Commit a1009d6

Browse files
feat(overmind): safely stringify non plain objects
1 parent 30edfb2 commit a1009d6

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

packages/node_modules/overmind/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
"es"
3333
],
3434
"dependencies": {
35-
"proxy-state-tree": "next",
36-
"action-chain": "next",
3735
"@types/node": "^10.5.1",
36+
"action-chain": "next",
37+
"is-plain-object": "^2.0.4",
38+
"proxy-state-tree": "next",
3839
"tslib": "^1.9.3"
3940
},
4041
"devDependencies": {

packages/node_modules/overmind/src/Devtools.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
import * as isPlainObject from 'is-plain-object'
2+
13
export type Message = {
24
type: string
35
data?: object
46
}
57

8+
export function safeValue(value) {
9+
if (
10+
typeof value === 'object' &&
11+
!Array.isArray(value) &&
12+
value !== null &&
13+
!isPlainObject(value)
14+
) {
15+
return `[${value.constructor.name || 'NOT SERIALIZABLE'}]`
16+
}
17+
18+
return value
19+
}
20+
21+
export function safeValues(values) {
22+
return values.map(safeValue)
23+
}
24+
625
function debounce(func, wait) {
726
let timeout
827
return function() {
@@ -20,7 +39,7 @@ function debounce(func, wait) {
2039
}
2140

2241
export default class Devtools {
23-
buffer: Message[]
42+
buffer: string[]
2443
ws: WebSocket
2544
isConnected: boolean
2645
doReconnect: boolean
@@ -68,12 +87,12 @@ export default class Devtools {
6887
)
6988
}
7089
send(message: Message) {
71-
this.buffer.push(message)
90+
this.buffer.push(JSON.stringify(message))
7291
this.sendBuffer()
7392
}
7493
private sendBuffer = debounce(function() {
7594
if (this.isConnected) {
76-
this.ws.send(JSON.stringify(this.buffer))
95+
this.ws.send('[' + this.buffer.join(',') + ']')
7796
this.buffer.length = 0
7897
}
7998
}, 50)

packages/node_modules/overmind/src/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { actionChainFactory } from 'action-chain'
22
import ProxyStateTree from 'proxy-state-tree'
3-
import Devtools, { Message } from './Devtools'
3+
import Devtools, { Message, safeValue } from './Devtools'
44
import createActionFactory, {
55
Action,
66
NoValueAction,
@@ -77,10 +77,22 @@ export default class App<
7777
}
7878

7979
actionChain.on('operator:async', () => {
80-
proxyStateTree.flush()
80+
const mutations = proxyStateTree.flush()
81+
if (this.devtools) {
82+
this.devtools.send({
83+
type: 'flush',
84+
data: { mutations },
85+
})
86+
}
8187
})
8288
actionChain.on('action:end', () => {
83-
proxyStateTree.flush()
89+
const mutations = proxyStateTree.flush()
90+
if (this.devtools) {
91+
this.devtools.send({
92+
type: 'flush',
93+
data: { mutations },
94+
})
95+
}
8496
})
8597

8698
this.state = proxyStateTree.get()
@@ -134,7 +146,10 @@ export default class App<
134146
actionChain.on('operator:end', (data) =>
135147
devtools.send({
136148
type: 'operator:end',
137-
data,
149+
data: {
150+
...data,
151+
result: safeValue(data.result),
152+
},
138153
})
139154
)
140155
actionChain.on('action:end', (data) =>

0 commit comments

Comments
 (0)