Skip to content

Commit 3f5e461

Browse files
fix(overmind): fix lazy loading namespacePath and typing
1 parent e91df73 commit 3f5e461

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Overmind } from '../'
1+
import { Overmind, createOvermindMock } from '../'
22
import { lazy, merge, namespaced } from './'
33

44
describe('Config', () => {
@@ -94,4 +94,29 @@ describe('Config', () => {
9494
expect(merged.state.foo).toEqual('bar')
9595
expect(merged.state.configB.bar).toEqual('baz')
9696
})
97+
98+
test('should keep context when lazy loading namespaces', async () => {
99+
const config = {
100+
state: {
101+
foo: 'bar',
102+
},
103+
actions: {
104+
changeFoo(context) {
105+
context.state.config.foo = 'bar2'
106+
}
107+
}
108+
}
109+
110+
const overmind = createOvermindMock(lazy({
111+
config: () => Promise.resolve(config)
112+
}))
113+
114+
await overmind.onInitialize()
115+
116+
await overmind.actions.lazy.loadConfig('config')
117+
// @ts-ignore
118+
overmind.actions.config.changeFoo()
119+
expect(overmind.state.config!.foo).toEqual('bar2')
120+
121+
})
97122
})

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface LazyConfiguration {
1313
}>
1414
}
1515

16-
export function lazy<T extends LazyConfiguration, B = T>(
16+
export function lazy<T extends LazyConfiguration>(
1717
configurations: T
1818
): {
1919
onInitialize?: any
@@ -70,24 +70,28 @@ export function lazy<T extends LazyConfiguration, B = T>(
7070
},
7171
actions: {
7272
lazy: {
73-
loadConfig({ state, ...rest }, key) {
73+
loadConfig({ state, execution, ...rest }, key) {
7474
const configToLoad = configurations[key]
75-
75+
const namespacePath = execution.namespacePath.slice(0, execution.namespacePath.length - 1).concat(key)
7676
return configToLoad().then((loadedConfig) => {
7777
const newConfig = namespaced({
7878
[key]: loadedConfig,
7979
})
8080

8181
if (newConfig.state && newConfig.state[key])
82-
state[key] = processState(newConfig.state[key])
82+
state[key] = processState(newConfig.state[key]!)
8383
if (newConfig.effects && newConfig.effects[key])
8484
app.effects[key] = newConfig.effects[key]
8585
if (newConfig.actions && newConfig.actions[key])
86-
app.actions[key] = app.getActions(newConfig.actions[key])
86+
app.actions[key] = app.getActions(newConfig.actions[key], namespacePath)
8787
if (newConfig.onInitialize)
8888
newConfig.onInitialize(
8989
{
9090
state,
91+
execution: {
92+
...execution,
93+
namespacePath
94+
},
9195
...rest,
9296
},
9397
app

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
ITrackStateTree,
66
} from 'proxy-state-tree'
77

8-
import { IAction, IOperator, IState } from './types'
8+
import { IAction, IOperator } from './types'
99

1010
export type SubType<Base, Condition> = Pick<
1111
Base,
12-
{ [Key in keyof Base]: Base[Key] extends Condition ? Key : never }[keyof Base]
12+
{ [Key in keyof Base]: Base[Key] extends Condition | undefined ? Key : never }[keyof Base]
1313
>
1414

1515
export type NestedPartial<T> = T extends Function

0 commit comments

Comments
 (0)