Skip to content

Commit 00ce2d5

Browse files
feat(overmind): make components, derived and computed pass flushId to devtools
1 parent 543ba40 commit 00ce2d5

File tree

4 files changed

+148
-62
lines changed

4 files changed

+148
-62
lines changed

packages/node_modules/overmind/src/computed.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,32 @@ export class Computed {
5757
if (cache.proxyStateTreeListener) {
5858
cache.proxyStateTreeListener.update(cache.paths)
5959
} else {
60+
eventHub.emit('computed', {
61+
path,
62+
paths: Array.from(cache.paths),
63+
updateCount: cache.updateCount,
64+
value: cache.value,
65+
limit: this.cacheLimit,
66+
cacheKeysCount: this.cacheKeys.length,
67+
cacheKeyIndex: this.cacheKeys.indexOf(config),
68+
})
6069
cache.proxyStateTreeListener = proxyStateTree.addMutationListener(
6170
cache.paths,
62-
() => {
71+
(flushId) => {
72+
eventHub.emit('computed', {
73+
path,
74+
paths: Array.from(cache.paths),
75+
updateCount: cache.updateCount,
76+
value: cache.value,
77+
limit: this.cacheLimit,
78+
cacheKeysCount: this.cacheKeys.length,
79+
cacheKeyIndex: this.cacheKeys.indexOf(config),
80+
flushId,
81+
})
6382
cache.isDirty = true
6483
}
6584
)
6685
}
67-
eventHub.emit('computed', {
68-
path,
69-
paths: Array.from(cache.paths),
70-
updateCount: cache.updateCount,
71-
value: cache.value,
72-
limit: this.cacheLimit,
73-
cacheKeysCount: this.cacheKeys.length,
74-
cacheKeyIndex: this.cacheKeys.indexOf(config),
75-
})
7686
cache.updateCount++
7787

7888
// Tracks the paths for the consumer of this derived value

packages/node_modules/overmind/src/derived.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,26 @@ class Derived {
3030
if (this.proxyStateTreeListener) {
3131
this.proxyStateTreeListener.update(this.paths)
3232
} else {
33+
eventHub.emit('derived', {
34+
path,
35+
paths: Array.from(this.paths),
36+
updateCount: this.updateCount,
37+
value: this.value,
38+
})
3339
this.proxyStateTreeListener = proxyStateTree.addMutationListener(
3440
this.paths,
35-
() => {
41+
(flushId) => {
42+
eventHub.emit('derived', {
43+
path,
44+
paths: Array.from(this.paths),
45+
updateCount: this.updateCount,
46+
value: this.value,
47+
flushId,
48+
})
3649
this.isDirty = true
3750
}
3851
)
3952
}
40-
eventHub.emit('derived', {
41-
path,
42-
paths: Array.from(this.paths),
43-
updateCount: this.updateCount,
44-
value: this.value,
45-
})
4653
this.updateCount++
4754
}
4855

packages/node_modules/overmind/src/index.ts

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface Events {
2525
paths: string[]
2626
updateCount: number
2727
value: any
28+
flushId?: number
2829
}
2930
computed: {
3031
path: string
@@ -34,6 +35,7 @@ export interface Events {
3435
limit: number
3536
cacheKeysCount: number
3637
cacheKeyIndex: number
38+
flushId?: number
3739
}
3840
'reaction:add': {
3941
path: string
@@ -151,24 +153,35 @@ export default class App<
151153

152154
/*
153155
Identify when the state tree should flush out changes
156+
157+
TODO:
158+
- How should I know when an operator is connected to a flushId?
159+
- Pass in operatorId on :async and set flushId directly?
160+
- Store operatorId on the flush and check?
154161
*/
155-
actionChain.on('operator:async', () => {
162+
actionChain.on('operator:async', (data) => {
163+
const flushData = proxyStateTree.flush()
156164
if (this.devtools) {
157165
this.devtools.send({
158166
type: 'flush',
159-
data: { mutations: proxyStateTree.mutations },
167+
data: {
168+
...data,
169+
...flushData,
170+
},
160171
})
161172
}
162-
proxyStateTree.flush()
163173
})
164-
actionChain.on('action:end', () => {
174+
actionChain.on('action:end', (data) => {
175+
const flushData = proxyStateTree.flush()
165176
if (this.devtools) {
166177
this.devtools.send({
167178
type: 'flush',
168-
data: { mutations: proxyStateTree.mutations },
179+
data: {
180+
...data,
181+
...flushData,
182+
},
169183
})
170184
}
171-
proxyStateTree.flush()
172185
})
173186

174187
/*
@@ -197,7 +210,10 @@ export default class App<
197210
actionChain.on('action:start', (data) =>
198211
devtools.send({
199212
type: 'action:start',
200-
data,
213+
data: {
214+
...data,
215+
value: safeValue(data.value),
216+
},
201217
})
202218
)
203219
actionChain.on('operator:start', (data) =>
@@ -307,24 +323,39 @@ export default class App<
307323
}
308324
}
309325
private getActions(configuration, action) {
310-
return typeof configuration.actions === 'function'
311-
? (configuration.actions as ActionsCallback<Providers, State>)(
312-
action as IAction<State, Providers & { state: State }>
313-
)
314-
: (Object.keys(configuration.actions || {}).reduce(
315-
(aggr, namespace) =>
316-
Object.assign(
317-
aggr,
318-
configuration.actions[namespace]
319-
? {
320-
[namespace]: configuration.actions[namespace](
321-
action as IAction<State, Providers & { state: State }>
322-
),
323-
}
324-
: {}
325-
),
326-
{}
327-
) as any)
326+
const actions =
327+
typeof configuration.actions === 'function'
328+
? (configuration.actions as ActionsCallback<Providers, State>)(
329+
action as IAction<State, Providers & { state: State }>
330+
)
331+
: (Object.keys(configuration.actions || {}).reduce(
332+
(aggr, namespace) =>
333+
Object.assign(
334+
aggr,
335+
configuration.actions[namespace]
336+
? {
337+
[namespace]: configuration.actions[namespace](
338+
action as IAction<State, Providers & { state: State }>
339+
),
340+
}
341+
: {}
342+
),
343+
{}
344+
) as any)
345+
346+
if (this.devtools) {
347+
Object.keys(actions).forEach((key) => {
348+
if (typeof configuration.actions === 'function') {
349+
actions[key].displayName = key
350+
} else {
351+
Object.keys(actions[key]).forEach((subKey) => {
352+
actions[key][subKey].displayName = key + '.' + subKey
353+
})
354+
}
355+
})
356+
}
357+
358+
return actions
328359
}
329360
trackState() {
330361
return this.proxyStateTree.startPathsTracking()

packages/node_modules/overmind/src/views/react.ts

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,35 @@ export default class ReactApp<
100100
if (this.__mutationListener) {
101101
this.__mutationListener.update(paths)
102102
} else {
103-
this.__mutationListener = instance.addMutationListener(paths, () => {
104-
this.forceUpdate()
105-
})
103+
if (instance.devtools) {
104+
instance.devtools.send({
105+
type: 'component:add',
106+
data: {
107+
componentId,
108+
componentInstanceId: this.props.app.__componentInstanceId,
109+
name: Component.name || '',
110+
paths: Array.from(paths),
111+
},
112+
})
113+
}
114+
this.__mutationListener = instance.addMutationListener(
115+
paths,
116+
(flushId) => {
117+
if (instance.devtools) {
118+
instance.devtools.send({
119+
type: 'component:update',
120+
data: {
121+
componentId,
122+
componentInstanceId: this.props.app.__componentInstanceId,
123+
name: Component.name || '',
124+
paths: Array.from(paths),
125+
flushId,
126+
},
127+
})
128+
}
129+
this.forceUpdate()
130+
}
131+
)
106132
}
107133

108134
return value
@@ -152,26 +178,38 @@ export default class ReactApp<
152178
)
153179
const paths = instance.clearTrackState(trackId)
154180

155-
if (instance.devtools) {
156-
instance.devtools.send({
157-
type: this.__mutationListener
158-
? 'component:update'
159-
: 'component:add',
160-
data: {
161-
componentId,
162-
componentInstanceId: this.__componentInstanceId,
163-
name: Component.name || '',
164-
paths: Array.from(paths),
165-
},
166-
})
167-
}
168-
169181
if (this.__mutationListener) {
170182
this.__mutationListener.update(paths)
171183
} else {
172-
this.__mutationListener = instance.addMutationListener(paths, () => {
173-
this.forceUpdate()
174-
})
184+
if (instance.devtools) {
185+
instance.devtools.send({
186+
type: 'component:add',
187+
data: {
188+
componentId,
189+
componentInstanceId: this.__componentInstanceId,
190+
name: Component.name || '',
191+
paths: Array.from(paths),
192+
},
193+
})
194+
}
195+
this.__mutationListener = instance.addMutationListener(
196+
paths,
197+
(flushId) => {
198+
if (instance.devtools) {
199+
instance.devtools.send({
200+
type: 'component:update',
201+
data: {
202+
componentId,
203+
componentInstanceId: this.__componentInstanceId,
204+
name: Component.name || '',
205+
paths: Array.from(paths),
206+
flushId,
207+
},
208+
})
209+
}
210+
this.forceUpdate()
211+
}
212+
)
175213
}
176214

177215
return value

0 commit comments

Comments
 (0)