Skip to content

Commit 733a430

Browse files
refactor(overmind): clean up events and fire them async
1 parent 4e53493 commit 733a430

File tree

6 files changed

+114
-154
lines changed

6 files changed

+114
-154
lines changed

packages/node_modules/overmind/src/Action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class Action<
6868
this.proxyStateTree.startMutationTracking()
6969
cb(effects.state, value)
7070
const mutations = this.proxyStateTree.clearMutationTracking()
71-
this.getActionChain().emit('mutations', {
71+
this.getActionChain().emitAsync('mutations', {
7272
mutations,
7373
...effects.__execution,
7474
})

packages/node_modules/overmind/src/computed.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventEmitter } from 'betsy'
22
import ProxyStateTree from 'proxy-state-tree'
3-
import { Events } from './'
3+
import { Events, EventType } from './'
44

55
type ComputedOptions = {
66
cacheLimit?: number
@@ -60,7 +60,7 @@ export class Computed {
6060
cache.proxyStateTreeListener = proxyStateTree.addMutationListener(
6161
cache.paths,
6262
(flushId) => {
63-
eventHub.emit('computed:dirty', {
63+
eventHub.emitAsync(EventType.COMPUTED_DIRTY, {
6464
path,
6565
flushId,
6666
})
@@ -75,14 +75,15 @@ export class Computed {
7575
proxyStateTree.addTrackingPath(path)
7676
}
7777

78-
eventHub.emit('computed', {
78+
eventHub.emitAsync(EventType.COMPUTED, {
7979
path,
80-
paths: Array.from(cache.paths),
8180
updateCount: cache.updateCount,
82-
value: cache.value,
8381
limit: this.cacheLimit,
84-
cacheKeysCount: this.cacheKeys.length,
85-
cacheKeyIndex: this.cacheKeys.indexOf(config),
82+
cache: Array.from(this.cache.values()).map((cache) => ({
83+
value: cache.value,
84+
updateCount: cache.updateCount,
85+
paths: Array.from(cache.paths),
86+
})),
8687
})
8788

8889
return cache.value

packages/node_modules/overmind/src/derived.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventEmitter } from 'betsy'
22
import ProxyStateTree from 'proxy-state-tree'
3-
import { Events } from './'
3+
import { Events, EventType } from './'
44

55
class Derived {
66
isDirty: boolean
@@ -33,15 +33,15 @@ class Derived {
3333
this.proxyStateTreeListener = proxyStateTree.addMutationListener(
3434
this.paths,
3535
(flushId) => {
36-
eventHub.emit('derived:dirty', {
36+
eventHub.emitAsync(EventType.DERIVED_DIRTY, {
3737
path,
3838
flushId,
3939
})
4040
this.isDirty = true
4141
}
4242
)
4343
}
44-
eventHub.emit('derived', {
44+
eventHub.emitAsync(EventType.DERIVED, {
4545
path,
4646
paths: Array.from(this.paths),
4747
updateCount: this.updateCount,

packages/node_modules/overmind/src/index.ts

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,79 @@ type Options = {
1919
devtools?: string
2020
}
2121

22+
type CacheMessage = {
23+
value: any
24+
paths: string[]
25+
updateCount: number
26+
}
27+
28+
export enum EventType {
29+
DERIVED = 'derived',
30+
DERIVED_DIRTY = 'derived:dirty',
31+
COMPUTED = 'computed',
32+
COMPUTED_DIRTY = 'computed:dirty',
33+
REACTION_ADD = 'reaction:add',
34+
REACTION_UPDATE = 'reaction:update',
35+
REACTION_REMOVE = 'reaction:remove',
36+
COMPONENT_ADD = 'component:add',
37+
COMPONENT_UPDATE = 'component:update',
38+
COMPONENT_REMOVE = 'component:remove',
39+
}
40+
2241
export interface Events {
23-
derived: {
42+
[EventType.DERIVED]: {
2443
path: string
2544
paths: string[]
2645
updateCount: number
2746
value: any
2847
}
29-
'derived:dirty': {
48+
[EventType.DERIVED_DIRTY]: {
3049
path: string
3150
flushId: number
3251
}
33-
computed: {
52+
[EventType.COMPUTED]: {
3453
path: string
35-
paths: string[]
3654
updateCount: number
37-
value: any
55+
cache: CacheMessage[]
3856
limit: number
39-
cacheKeysCount: number
40-
cacheKeyIndex: number
4157
}
42-
'computed:dirty': {
58+
[EventType.COMPUTED_DIRTY]: {
4359
path: string
4460
flushId: number
4561
}
46-
'reaction:add': {
62+
[EventType.REACTION_ADD]: {
4763
path: string
4864
statePath: string
4965
updateCount: number
5066
}
51-
'reaction:update': {
67+
[EventType.REACTION_UPDATE]: {
5268
path: string
5369
statePath: string
5470
updateCount: number
5571
}
56-
'reaction:remove': {
72+
[EventType.REACTION_REMOVE]: {
5773
path: string
5874
statePath: string
5975
updateCount: number
6076
}
77+
[EventType.COMPONENT_ADD]: {
78+
componentId: number
79+
componentInstanceId: number
80+
name: string
81+
paths: string[]
82+
}
83+
[EventType.COMPONENT_UPDATE]: {
84+
componentId: number
85+
componentInstanceId: number
86+
name: string
87+
paths: string[]
88+
flushId: number
89+
}
90+
[EventType.COMPONENT_REMOVE]: {
91+
componentId: number
92+
componentInstanceId: number
93+
name: string
94+
}
6195
}
6296

6397
export type ActionsCallback<Effects, State> = (
@@ -94,7 +128,7 @@ export default class App<
94128
}
95129
> {
96130
private proxyStateTree: ProxyStateTree
97-
private eventHub: EventEmitter<Events>
131+
eventHub: EventEmitter<Events>
98132
devtools: Devtools
99133
actions: Actions extends ActionsCallback<Effects, State>
100134
? ReturnType<Actions>
@@ -207,12 +241,6 @@ export default class App<
207241
// To use for communication from devtools app
208242
}
209243
)
210-
devtools.send({
211-
type: 'init',
212-
data: {
213-
state: proxyStateTree.get(),
214-
},
215-
})
216244
actionChain.on('action:start', (data) =>
217245
devtools.send({
218246
type: 'action:start',
@@ -255,48 +283,22 @@ export default class App<
255283
data,
256284
})
257285
)
258-
eventHub.on('derived', (data) =>
259-
devtools.send({
260-
type: 'derived',
261-
data,
262-
})
263-
)
264-
eventHub.on('derived:dirty', (data) =>
265-
devtools.send({
266-
type: 'derived:dirty',
267-
data,
268-
})
269-
)
270-
eventHub.on('computed', (data) =>
271-
devtools.send({
272-
type: 'computed',
273-
data,
274-
})
275-
)
276-
eventHub.on('computed:dirty', (data) =>
277-
devtools.send({
278-
type: 'computed:dirty',
279-
data,
280-
})
281-
)
282-
eventHub.on('reaction:add', (data) =>
283-
devtools.send({
284-
type: 'reaction:add',
285-
data,
286-
})
287-
)
288-
eventHub.on('reaction:update', (data) =>
289-
devtools.send({
290-
type: 'reaction:update',
291-
data,
292-
})
293-
)
294-
eventHub.on('reaction:remove', (data) =>
295-
devtools.send({
296-
type: 'reaction:remove',
297-
data,
298-
})
299-
)
286+
for (let type in EventType) {
287+
eventHub.on(EventType[type], (data) =>
288+
devtools.send({
289+
type: EventType[type],
290+
data,
291+
})
292+
)
293+
}
294+
// This message is always the first as it is passed synchronously, all other
295+
// events are emitted async
296+
devtools.send({
297+
type: 'init',
298+
data: {
299+
state: proxyStateTree.get(),
300+
},
301+
})
300302
this.devtools = devtools
301303
}
302304
private initializeReactions(configuration, eventHub, proxyStateTree, action) {

packages/node_modules/overmind/src/reaction.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventEmitter } from 'betsy'
22
import ProxyStateTree from 'proxy-state-tree'
3-
import { Events } from './'
3+
import { Events, EventType } from './'
44
import { INoValueAction } from './Action'
55

66
export type ReactionConfig<State, Providers> = (
@@ -38,7 +38,7 @@ class Reaction<State, Providers> {
3838
for (let mutationIndex in mutations) {
3939
const mutation = mutations[mutationIndex]
4040
if (mutation.path.indexOf(this.statePath) === 0) {
41-
this.eventHub.emit('reaction:update', {
41+
this.eventHub.emitAsync(EventType.REACTION_UPDATE, {
4242
path: this.path,
4343
statePath: this.statePath,
4444
updateCount: ++this.updateCount,
@@ -49,14 +49,14 @@ class Reaction<State, Providers> {
4949
}
5050
)
5151

52-
this.eventHub.emit('reaction:add', {
52+
this.eventHub.emitAsync(EventType.REACTION_ADD, {
5353
path: this.path,
5454
statePath: this.statePath,
5555
updateCount: this.updateCount,
5656
})
5757
}
5858
destroy() {
59-
this.eventHub.emit('reaction:remove', {
59+
this.eventHub.emitAsync(EventType.REACTION_REMOVE, {
6060
path: this.path,
6161
statePath: this.statePath,
6262
updateCount: this.updateCount,

0 commit comments

Comments
 (0)