Skip to content

Commit efe6e12

Browse files
fix(overmind): properly extract any nested action
1 parent 38b94e4 commit efe6e12

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export class Overmind<ThisConfig extends IConfiguration>
345345
/*
346346
Expose the created actions
347347
*/
348-
this.actions = this.getActions(configuration)
348+
this.actions = this.getActions(configuration.actions)
349349

350350
if (mode.mode === MODE_DEFAULT && configuration.onInitialize) {
351351
const onInitialize = this.createAction(
@@ -781,51 +781,22 @@ export class Overmind<ThisConfig extends IConfiguration>
781781
return aggr
782782
}, {})
783783
}
784-
private getActions(configuration: IConfiguration) {
785-
let actions = {}
786-
if (configuration.actions) {
787-
actions = configuration.actions
788-
}
789-
790-
const evaluatedActions = Object.keys(actions).reduce((aggr, name) => {
784+
private getActions(actions: any = {}, path: string[] = []) {
785+
return Object.keys(actions).reduce((aggr, name) => {
791786
if (typeof actions[name] === 'function') {
787+
const action = this.createAction(name, actions[name]) as any
788+
789+
action.displayName = path.concat(name).join('.')
790+
792791
return Object.assign(aggr, {
793-
[name]: this.createAction(name, actions[name]),
792+
[name]: action,
794793
})
795794
}
796795

797796
return Object.assign(aggr, {
798-
[name]: Object.keys(actions[name] || {}).reduce(
799-
(aggr, subName) =>
800-
Object.assign(
801-
aggr,
802-
typeof actions[name][subName] === 'function'
803-
? {
804-
[subName]: this.createAction(
805-
subName,
806-
actions[name][subName]
807-
),
808-
}
809-
: {}
810-
),
811-
{}
812-
),
797+
[name]: this.getActions(actions[name], path.concat(name)),
813798
})
814799
}, {}) as any
815-
816-
if (this.devtools) {
817-
Object.keys(evaluatedActions).forEach((key) => {
818-
if (typeof evaluatedActions[key] === 'function') {
819-
evaluatedActions[key].displayName = key
820-
} else {
821-
Object.keys(evaluatedActions[key]).forEach((subKey) => {
822-
evaluatedActions[key][subKey].displayName = key + '.' + subKey
823-
})
824-
}
825-
})
826-
}
827-
828-
return evaluatedActions
829800
}
830801
getTrackStateTree() {
831802
return this.proxyStateTree.getTrackStateTree()
@@ -852,7 +823,7 @@ export class Overmind<ThisConfig extends IConfiguration>
852823
this.proxyStateTree.sourceState = this.getState(mergedConfiguration)
853824
proxyStateTree.createTrackStateProxifier()
854825
this.state = this.proxyStateTree.state as any
855-
this.actions = this.getActions(mergedConfiguration)
826+
this.actions = this.getActions(mergedConfiguration.actions)
856827
this.effects = mergedConfiguration.effects || {}
857828

858829
this.proxyStateTree.forceFlush()

packages/node_modules/overmind/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ export function createActionsProxy(actions, cb) {
115115
return cb(target[prop])
116116
}
117117

118+
if (!target[prop]) {
119+
throw new Error(
120+
`You are pointing towards an action that does not exist. "${String(
121+
prop
122+
)}" does not exist`
123+
)
124+
}
125+
118126
return createActionsProxy(target[prop], cb)
119127
},
120128
})

0 commit comments

Comments
 (0)