Skip to content

Commit b93738b

Browse files
feat(overmind): add dynamic module as experimental feature
1 parent 34b0b79 commit b93738b

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

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

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import App, { TAction, TConfig } from './'
1+
import App, { TAction, TConfig, DynamicAction, dynamicModule } from './'
22

33
describe('Overmind', () => {
44
test('should instantiate app with state', () => {
@@ -35,7 +35,6 @@ describe('Overmind', () => {
3535
bar: fooAction,
3636
},
3737
}
38-
3938
type Config = TConfig<{
4039
modules: {
4140
foo: typeof foo
@@ -59,52 +58,33 @@ describe('Overmind', () => {
5958
expect(app.actions.foo.foo('mip')).toEqual('mip')
6059
expect(app.actions.bar.bar('bop')).toEqual('bop')
6160
})
62-
/*
6361
test('should allow namespaced modules to be functions', () => {
64-
const fooNamespace = (namespace: string) => {
65-
type Action<InitialValue = void> = TAction<InitialValue, Namespace>
66-
type Map<InValue = void, OutValue = InValue> = TOperation.Map<
67-
InValue,
68-
OutValue,
69-
Namespace
70-
>
71-
72-
type State = {
73-
foo: string
74-
}
75-
76-
const state: State = {
77-
foo: 'bar',
78-
}
79-
80-
const effects = {
81-
hello: () => 'hello',
82-
}
83-
84-
const mapFoo: Map<string> = ({ state }, value) =>
85-
value + (state[namespace] as State).foo
86-
87-
const foo: Action<string> = (action) => action<string>().map(mapFoo)
62+
const foo = dynamicModule((namespace) => {
63+
const fooAction: DynamicAction<string> = (action) =>
64+
action().mutate((state) => (state[namespace].foo = 'bar2'))
8865

8966
return {
90-
state,
67+
state: {
68+
foo: 'bar',
69+
},
9170
actions: {
92-
foo,
71+
foo: fooAction,
9372
},
94-
effects,
9573
}
96-
}
97-
98-
const config = namespaces({
99-
foo: fooNamespace,
10074
})
10175

76+
const config = {
77+
modules: {
78+
foo,
79+
},
80+
}
81+
10282
const app = new App(config)
10383

10484
expect(app.state.foo.foo).toEqual('bar')
105-
expect(app.actions.foo.foo('mip')).toEqual('mipbar')
85+
expect(app.actions.foo.foo('mip')).toEqual('mip')
86+
expect(app.state.foo.foo).toEqual('bar2')
10687
})
107-
*/
10888
})
10989

11090
describe('OPERATORS', () => {

packages/node_modules/overmind/src/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,24 @@ export { IValueAction, Compose }
1212
export const log = (...objects: any[]) =>
1313
console.log(...objects.map((obj) => JSON.parse(JSON.stringify(obj))))
1414

15+
export const dynamicModule = ((cb) => (namespace) =>
16+
cb(namespace)) as DynamicModule
17+
1518
/*
1619
BASE TYPES
1720
*/
21+
type DynamicModule = <
22+
T extends {
23+
onInitialize?: any
24+
state?: any
25+
effects?: any
26+
actions?: any
27+
reactions?: any
28+
}
29+
>(
30+
cb: (namespace: string) => T
31+
) => ReturnType<typeof cb>
32+
1833
export type Configuration = {
1934
onInitialize?: any
2035
state?: any
@@ -106,6 +121,13 @@ export type Action<InitialValue = void, ReturnValue = any> = Compose<
106121
ReturnValue
107122
>
108123

124+
export type DynamicAction<InitialValue = void, ReturnValue = any> = Compose<
125+
any,
126+
any,
127+
InitialValue,
128+
ReturnValue
129+
>
130+
109131
export type Reaction = (
110132
reaction: (
111133
getState: (state: IApp['state']) => any,
@@ -338,6 +360,10 @@ export default class App<
338360
}
339361
state: EvalConfig['state']
340362
constructor(configuration: Config, options: Options = {}) {
363+
/*
364+
Mutate module functions into module objects
365+
*/
366+
this.mutateModuleFunctionsIntoModules(configuration)
341367
/*
342368
Set up an eventHub to trigger information from derived, computed and reactions
343369
*/
@@ -453,6 +479,13 @@ export default class App<
453479
onInitialize.displayName = 'onInitialize'
454480
onInitialize(undefined)
455481
}
482+
private mutateModuleFunctionsIntoModules(config: Configuration) {
483+
if (config.modules) {
484+
Object.keys(config.modules).forEach((key) => {
485+
config.modules[key] = (config.modules[key] as any)(key)
486+
})
487+
}
488+
}
456489
private initializeDevtools(host, actionChain, eventHub, proxyStateTree) {
457490
const devtools = new Devtools()
458491
devtools.connect(

0 commit comments

Comments
 (0)