Skip to content

Commit 47ffc6c

Browse files
fix(overmind): lazy loading modules fixed
1 parent 0c57f48 commit 47ffc6c

File tree

3 files changed

+68
-40
lines changed

3 files changed

+68
-40
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { IConfiguration, IAction } from '../'
2+
import { processState } from '../utils'
23

34
type SubType<Base, Condition> = Pick<
45
Base,
@@ -112,17 +113,35 @@ export function merge(...configurations: IConfiguration[]): IConfiguration {
112113

113114
const reducedConfigurations = configurations.reduce(
114115
(aggr, config) => {
115-
const stateDuplicates = aggr.state ? Object.keys(aggr.state).some( key => config.state ? Object.keys(config.state).includes(key) : false) : false;
116-
const actionsDuplicates = aggr.actions ? Object.keys(aggr.actions).some( key => config.actions ? Object.keys(config.actions).includes(key) : false) : false;
117-
const effectsDuplicates = aggr.effects ? Object.keys(aggr.effects).some( key => config.effects ? Object.keys(config.effects).includes(key) : false) : false;
118-
if (stateDuplicates){
119-
throw new Error("Merge conflict: at least one state definition contains a duplicate key")
116+
const stateDuplicates = aggr.state
117+
? Object.keys(aggr.state).some((key) =>
118+
config.state ? Object.keys(config.state).includes(key) : false
119+
)
120+
: false
121+
const actionsDuplicates = aggr.actions
122+
? Object.keys(aggr.actions).some((key) =>
123+
config.actions ? Object.keys(config.actions).includes(key) : false
124+
)
125+
: false
126+
const effectsDuplicates = aggr.effects
127+
? Object.keys(aggr.effects).some((key) =>
128+
config.effects ? Object.keys(config.effects).includes(key) : false
129+
)
130+
: false
131+
if (stateDuplicates) {
132+
throw new Error(
133+
'Merge conflict: at least one state definition contains a duplicate key'
134+
)
120135
}
121136
if (actionsDuplicates) {
122-
throw new Error("Merge conflict: at least one actions definition contains a duplicate key")
137+
throw new Error(
138+
'Merge conflict: at least one actions definition contains a duplicate key'
139+
)
123140
}
124141
if (effectsDuplicates) {
125-
throw new Error("Merge conflict: at least one effects definition contains a duplicate key")
142+
throw new Error(
143+
'Merge conflict: at least one effects definition contains a duplicate key'
144+
)
126145
}
127146
return {
128147
onInitialize: aggr.onInitialize,
@@ -290,13 +309,14 @@ export function lazy<T extends LazyConfiguration, B = T>(
290309
lazy: {
291310
loadConfig({ state, ...rest }, key) {
292311
const configToLoad = configurations[key]
293-
configToLoad().then((loadedConfig) => {
312+
313+
return configToLoad().then((loadedConfig) => {
294314
const newConfig = namespaced({
295315
[key]: loadedConfig,
296316
})
297317

298318
if (newConfig.state && newConfig.state[key])
299-
state[key] = newConfig.state[key]
319+
state[key] = processState(newConfig.state[key])
300320
if (newConfig.effects && newConfig.effects[key])
301321
app.effects[key] = newConfig.effects[key]
302322
if (newConfig.actions && newConfig.actions[key])

packages/node_modules/overmind/src/index.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
getActionPaths,
4545
createActionsProxy,
4646
EXECUTION,
47+
processState,
4748
} from './utils'
4849
import {
4950
operatorStarted,
@@ -773,41 +774,16 @@ export class Overmind<ThisConfig extends IConfiguration>
773774
private getState(configuration: IConfiguration) {
774775
let state = {}
775776
if (configuration.state) {
776-
state = this.processState(configuration.state)
777+
state = processState(
778+
configuration.state,
779+
process.env.NODE_ENV === 'development'
780+
? this.derivedReferences
781+
: undefined
782+
)
777783
}
778784

779785
return state
780786
}
781-
private processState(state: {}) {
782-
return Object.keys(state).reduce((aggr, key) => {
783-
if (key === '__esModule') {
784-
return aggr
785-
}
786-
const originalDescriptor = Object.getOwnPropertyDescriptor(state, key)
787-
788-
if (originalDescriptor && 'get' in originalDescriptor) {
789-
Object.defineProperty(aggr, key, originalDescriptor as any)
790-
791-
return aggr
792-
}
793-
794-
const value = state[key]
795-
796-
if (isPlainObject(value)) {
797-
aggr[key] = this.processState(value)
798-
} else if (typeof value === 'function') {
799-
aggr[key] = new Derived(value)
800-
801-
if (process.env.NODE_ENV === 'development') {
802-
this.derivedReferences.push(aggr[key])
803-
}
804-
} else {
805-
Object.defineProperty(aggr, key, originalDescriptor as any)
806-
}
807-
808-
return aggr
809-
}, {})
810-
}
811787
private getActions(actions: any = {}, path: string[] = []) {
812788
return Object.keys(actions).reduce((aggr, name) => {
813789
if (typeof actions[name] === 'function') {

packages/node_modules/overmind/src/utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import isPlainObject from 'is-plain-obj'
22
import { IMutation } from 'proxy-state-tree'
33
import { safeValues } from './Devtools'
4+
import { Derived } from './derived'
45

56
export const IS_TEST = process.env.NODE_ENV === 'test'
67
export const IS_OPERATOR = Symbol('operator')
@@ -15,6 +16,37 @@ export class MockedEventEmitter {
1516
addListener() {}
1617
}
1718

19+
export function processState(state: {}, derivedReferences?: any[]) {
20+
return Object.keys(state).reduce((aggr, key) => {
21+
if (key === '__esModule') {
22+
return aggr
23+
}
24+
const originalDescriptor = Object.getOwnPropertyDescriptor(state, key)
25+
26+
if (originalDescriptor && 'get' in originalDescriptor) {
27+
Object.defineProperty(aggr, key, originalDescriptor as any)
28+
29+
return aggr
30+
}
31+
32+
const value = state[key]
33+
34+
if (isPlainObject(value)) {
35+
aggr[key] = this.processState(value)
36+
} else if (typeof value === 'function') {
37+
aggr[key] = new Derived(value)
38+
39+
if (derivedReferences) {
40+
derivedReferences.push(aggr[key])
41+
}
42+
} else {
43+
Object.defineProperty(aggr, key, originalDescriptor as any)
44+
}
45+
46+
return aggr
47+
}, {})
48+
}
49+
1850
export function getFunctionName(func: Function) {
1951
return func.name || (func as any).displayName || ''
2052
}

0 commit comments

Comments
 (0)