Skip to content

Commit 57d7897

Browse files
fix(overmind): replace actions on hot reloading instead of overriding
1 parent 824fe35 commit 57d7897

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

packages/node_modules/overmind/src/index.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('Overmind', () => {
170170
})
171171
app.eventHub.once(EventType.ACTION_START, (data) => {
172172
expect(toJSON(data)).toEqual({
173-
actionId: 0,
173+
actionId:'doThis',
174174
actionName: 'doThis',
175175
isRunning: true,
176176
namespacePath: [],
@@ -183,7 +183,7 @@ describe('Overmind', () => {
183183
})
184184
app.eventHub.once(EventType.ACTION_END, (data) => {
185185
expect(toJSON(data)).toEqual({
186-
actionId: 0,
186+
actionId: 'doThis',
187187
isRunning: false,
188188
executionId: 0,
189189
actionName: 'doThis',
@@ -204,7 +204,7 @@ describe('Overmind', () => {
204204
})
205205
app.eventHub.once(EventType.OPERATOR_START, (data) => {
206206
expect(toJSON(data)).toEqual({
207-
actionId: 0,
207+
actionId: 'doThis',
208208
isRunning: true,
209209
actionName: 'doThis',
210210
path: [],
@@ -216,7 +216,7 @@ describe('Overmind', () => {
216216
})
217217
app.eventHub.once(EventType.OPERATOR_END, (data) => {
218218
expect(toJSON(data)).toEqual({
219-
actionId: 0,
219+
actionId: 'doThis',
220220
isRunning: false,
221221
actionName: 'doThis',
222222
path: [],
@@ -234,7 +234,7 @@ describe('Overmind', () => {
234234
const app = createDefaultOvermind()
235235
app.eventHub.once(EventType.MUTATIONS, (data) => {
236236
expect(toJSON(data)).toEqual({
237-
actionId: 2,
237+
actionId: 'changeFoo',
238238
isRunning: true,
239239
actionName: 'changeFoo',
240240
mutations: [
@@ -260,7 +260,7 @@ describe('Overmind', () => {
260260
const app = createDefaultOvermind()
261261
app.eventHub.on(EventType.MUTATIONS, (data) => {
262262
expect(toJSON(data)).toEqual({
263-
actionId: 5,
263+
actionId: 'waitAndChangeFoo',
264264
isRunning: true,
265265
actionName: 'waitAndChangeFoo',
266266
mutations: [
@@ -286,7 +286,7 @@ describe('Overmind', () => {
286286
const app = createDefaultOvermind()
287287
app.eventHub.on(EventType.MUTATIONS, (data) => {
288288
expect(toJSON(data)).toEqual({
289-
actionId: 0,
289+
actionId: 'asyncChangeFoo',
290290
isRunning: true,
291291
actionName: 'asyncChangeFoo',
292292
mutations: [

packages/node_modules/overmind/src/index.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ const hotReloadingCache = {}
187187
export class Overmind<ThisConfig extends IConfiguration>
188188
implements IConfiguration {
189189
private proxyStateTree: ProxyStateTree<object>
190-
private actionReferences: Function[] = []
190+
private actionReferences: { [path: string]: Function } = {}
191191
private nextExecutionId: number = 0
192192
private mode: DefaultMode | TestMode | SSRMode
193193
private reydrateMutationsForHotReloading: IMutation[] = []
@@ -433,7 +433,7 @@ export class Overmind<ThisConfig extends IConfiguration>
433433
const execution = {
434434
[EXECUTION]: true,
435435
namespacePath,
436-
actionId: this.actionReferences.indexOf(action),
436+
actionId: name,
437437
executionId: this.nextExecutionId++,
438438
actionName: name,
439439
operatorId: 0,
@@ -519,9 +519,10 @@ export class Overmind<ThisConfig extends IConfiguration>
519519
private addExecutionMutation(mutation: IMutation) {
520520
;((this as unknown) as OvermindMock<Config>).mutations.push(mutation)
521521
}
522-
private createAction(name, action) {
523-
this.actionReferences.push(action)
522+
private createAction(name, originalAction) {
523+
this.actionReferences[name] = originalAction
524524
const actionFunc = (value?, boundExecution?: Execution) => {
525+
const action = this.actionReferences[name]
525526
// Developer might unintentionally pass more arguments, so have to ensure
526527
// that it is an actual execution
527528
boundExecution =
@@ -882,6 +883,36 @@ export class Overmind<ThisConfig extends IConfiguration>
882883
})
883884
}, {}) as any
884885
}
886+
/*
887+
Related to hot reloading we update the existing action references and add any new
888+
actions.
889+
*/
890+
private updateActions(actions: any = {}, path: string[] = []) {
891+
Object.keys(actions).forEach((name) => {
892+
if (typeof actions[name] === 'function') {
893+
const actionName = path.concat(name).join('.')
894+
if (this.actionReferences[actionName]) {
895+
this.actionReferences[actionName] = actions[name]
896+
} else {
897+
const target = path.reduce((aggr, key) => {
898+
if (!aggr[key]) {
899+
aggr[key] = {}
900+
}
901+
902+
return aggr[key]
903+
}, this.actions)
904+
target[name] = this.createAction(
905+
actionName,
906+
actions[name]
907+
) as any
908+
909+
target[name].displayName = path.concat(name).join('.')
910+
}
911+
} else {
912+
this.updateActions(actions[name], path.concat(name))
913+
}
914+
}, {}) as any
915+
}
885916
getTrackStateTree(): ITrackStateTree<any> {
886917
return this.proxyStateTree.getTrackStateTree()
887918
}
@@ -949,7 +980,8 @@ export class Overmind<ThisConfig extends IConfiguration>
949980
}
950981
reconfigure(configuration: IConfiguration) {
951982
const changeMutations = getChangeMutations(this.originalConfiguration.state, configuration.state || {})
952-
this.actions = this.getActions(configuration.actions)
983+
984+
this.updateActions(configuration.actions)
953985
this.effects = configuration.effects || {}
954986

955987
const mutationTree = this.proxyStateTree.getMutationTree()

0 commit comments

Comments
 (0)