Skip to content

Commit a93c11d

Browse files
feat(overmind): make onInitialize a callback with app instance
BREAKING CHANGE: onInitialize is now a callback with app instance
1 parent 9c8aa82 commit a93c11d

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"no-unused-expressions": "off",
3737
"no-use-before-define": "off",
3838
"no-useless-constructor": "off",
39-
"typescript/no-unused-vars": "error"
39+
"typescript/no-unused-vars": "error",
40+
"standard/no-callback-literal": "off"
4041
}
4142
}
4243
]

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ describe('Overmind', () => {
1414

1515
expect(app.state.foo).toEqual('bar')
1616
})
17+
test('should instantiate app with onInitialize', () => {
18+
const app = new App({
19+
onInitialize: (app) => {
20+
expect(app.state.foo).toBe('bar')
21+
expect(typeof app.actions.doThis === 'function')
22+
return 'foo'
23+
},
24+
state: {
25+
foo: 'bar',
26+
},
27+
actions: {
28+
doThis: (action) => action.run(() => {}),
29+
},
30+
effects: {},
31+
})
32+
33+
return app.initialized.then((value) => {
34+
expect(value).toEqual('foo')
35+
})
36+
})
1737
test('should instantiate app with modules', () => {
1838
const fooAction: Action<string> = ({ run }) => run(() => {})
1939

@@ -66,9 +86,31 @@ describe('Overmind', () => {
6686
expect(app.actions.foo.foo('mip')).toEqual('mip')
6787
expect(app.actions.bar.bar('bop')).toEqual('bop')
6888
})
89+
test('should instantiate modules with onInitialize', () => {
90+
const app = new App(
91+
modules({
92+
modules: {
93+
foo: {
94+
onInitialize: () => {
95+
return 'foo'
96+
},
97+
},
98+
bar: {
99+
onInitialize: () => {
100+
return 'bar'
101+
},
102+
},
103+
},
104+
})
105+
)
106+
107+
return app.initialized.then((values) => {
108+
expect(values).toEqual(['foo', 'bar'])
109+
})
110+
})
69111
})
70112

71-
describe.only('OPERATORS', () => {
113+
describe('OPERATORS', () => {
72114
test('fork', () => {
73115
expect.assertions(2)
74116
let calledFoo = false

packages/node_modules/overmind/src/index.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface IState {}
4040

4141
export interface IEffects {}
4242

43+
export interface IActions {}
44+
4345
interface IApp {
4446
effects: IEffects
4547
state: StateNode<IState>
@@ -49,6 +51,26 @@ interface IOperationArgument<Value> extends IApp {
4951
value: Value
5052
}
5153

54+
export type OnInitialize = (
55+
app: {
56+
state: IState
57+
actions: {
58+
[T in keyof IActions]: IActions[T] extends (...args: any[]) => any
59+
? ReturnType<IActions[T]>
60+
: {
61+
[P in keyof IActions[T]]: IActions[T][P] extends (
62+
...args: any[]
63+
) => any
64+
? ReturnType<IActions[T][P]>
65+
: undefined
66+
}
67+
}
68+
trackState(): number
69+
clearTrackState(id: number, cb?: () => void): Set<string>
70+
addMutationListener(paths, cb): () => void
71+
}
72+
) => void
73+
5274
type StateNode<State extends object> = {
5375
[P in keyof State]: State[P] extends Derive<any>
5476
? ReturnType<State[P]>
@@ -61,6 +83,8 @@ export type TState<Config extends Configuration> = Config['state']
6183

6284
export type TEffects<Config extends Configuration> = Config['effects']
6385

86+
export type TActions<Config extends Configuration> = Config['actions']
87+
6488
export type Mutate<Value = any> = (
6589
arg: { state: IApp['state']; value: Value }
6690
) => void
@@ -116,6 +140,7 @@ export type Compute<Input, Output> = (
116140
) => (state: IApp['state']) => Output
117141

118142
export type TConfig<Config extends Configuration> = {
143+
onInitialize: any
119144
state: Config['state'] & {}
120145
effects: Config['effects'] & {}
121146
actions: Config['actions'] & {}
@@ -143,6 +168,7 @@ export default class App<
143168
EvalConfig extends TConfig<Config>
144169
> {
145170
private proxyStateTree: ProxyStateTree
171+
initialized: Promise<any>
146172
eventHub: EventEmitter<Events>
147173
devtools: Devtools
148174
actions: {
@@ -249,12 +275,9 @@ export default class App<
249275
this.proxyStateTree = proxyStateTree
250276
this.eventHub = eventHub
251277

252-
if (configuration.onInitialize) {
253-
const onInitialize = operators.compose(configuration.onInitialize)
254-
// @ts-ignore
255-
onInitialize.displayName = 'onInitialize'
256-
onInitialize(undefined)
257-
}
278+
this.initialized = Promise.resolve(
279+
configuration.onInitialize ? configuration.onInitialize(this) : null
280+
)
258281
}
259282
private initializeDevtools(host, actionChain, eventHub, proxyStateTree) {
260283
const devtools = new Devtools(

packages/node_modules/overmind/src/modules.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ function parseModule(
9191
result.state[modName] = state
9292
}
9393
if (onInitialize) {
94-
// @ts-ignore
95-
onInitialize.displayName = modName + '.onInitialize'
9694
result.initializers.push(onInitialize)
9795
}
9896
}
@@ -117,12 +115,15 @@ export function modules<T extends ConfigurationWithModules>(
117115
parseModule(result, modName, modules[modName])
118116
})
119117

120-
const onInitialize: Action<undefined> = configWithModules.onInitialize
121-
? configWithModules.onInitialize
122-
: (action) => action.parallel(result.initializers)
118+
const onInitialize = (app) => {
119+
const allInitializers = [
120+
configWithModules.onInitialize && configWithModules.onInitialize(app),
121+
...result.initializers.map((initializer) => initializer(app)),
122+
].filter((initializer) => !!initializer)
123+
124+
return Promise.all(allInitializers)
125+
}
123126

124-
// @ts-ignore
125-
onInitialize.displayName = 'onInitialize'
126127
return {
127128
onInitialize,
128129
actions: result.actions,

0 commit comments

Comments
 (0)