Skip to content

Commit afb6e81

Browse files
committed
fix(overmind): fix onInitialize to use the same api as other actions
1 parent c85dc03 commit afb6e81

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import App, { TAction, TConfig, modules } from './'
1+
import App, { Action, TAction, TConfig, modules } from './'
22

33
describe('Overmind', () => {
44
test('should instantiate app with state', () => {
@@ -15,12 +15,22 @@ describe('Overmind', () => {
1515
expect(app.state.foo).toEqual('bar')
1616
})
1717
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')
18+
interface AppType {
19+
state: {
20+
foo: string
21+
}
22+
actions: {
23+
doThis(): void
24+
}
25+
}
26+
const onInitialize: Action<AppType, string> = (action) =>
27+
action.map(({ value }) => {
28+
expect(value.state.foo).toBe('bar')
29+
expect(typeof value.actions.doThis === 'function')
2230
return 'foo'
23-
},
31+
})
32+
const app = new App({
33+
onInitialize,
2434
state: {
2535
foo: 'bar',
2636
},
@@ -87,25 +97,26 @@ describe('Overmind', () => {
8797
expect(app.actions.bar.bar('bop')).toEqual('bop')
8898
})
8999
test('should instantiate modules with onInitialize', () => {
100+
const result = []
90101
const app = new App(
91102
modules({
92103
modules: {
93104
foo: {
94-
onInitialize: () => {
95-
return 'foo'
105+
onInitialize: (action) => () => {
106+
result.push('foo')
96107
},
97108
},
98109
bar: {
99-
onInitialize: () => {
100-
return 'bar'
110+
onInitialize: (action) => () => {
111+
result.push('bar')
101112
},
102113
},
103114
},
104115
})
105116
)
106117

107-
return app.initialized.then((values) => {
108-
expect(values).toEqual(['foo', 'bar'])
118+
return app.initialized.then(() => {
119+
expect(result).toEqual(['foo', 'bar'])
109120
})
110121
})
111122
})

packages/node_modules/overmind/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Derived from './derived'
1212
import Devtools, { Message, safeValue } from './Devtools'
1313
import { EventType, Events, Options } from './internalTypes'
1414
import Reaction from './reaction'
15+
1516
const isPlainObject = require('is-plain-object')
1617

1718
export { modules } from './modules'
@@ -275,9 +276,13 @@ export default class App<
275276
this.proxyStateTree = proxyStateTree
276277
this.eventHub = eventHub
277278

278-
this.initialized = Promise.resolve(
279-
configuration.onInitialize ? configuration.onInitialize(this) : null
280-
)
279+
if (configuration.onInitialize) {
280+
const onInitialize = configuration.onInitialize(operators)
281+
onInitialize.displayName = 'onInitialize'
282+
this.initialized = Promise.resolve(onInitialize(this))
283+
} else {
284+
this.initialized = Promise.resolve(null)
285+
}
281286
}
282287
private initializeDevtools(host, actionChain, eventHub, proxyStateTree) {
283288
const devtools = new Devtools(

packages/node_modules/overmind/src/modules.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,15 @@ export function modules<T extends ConfigurationWithModules>(
111111
}
112112
const modules = configWithModules.modules || {}
113113

114+
if (configWithModules.onInitialize) {
115+
result.initilizers.push(configWithModules.onInitialize)
116+
}
117+
114118
Object.keys(modules).forEach((modName) => {
115119
parseModule(result, modName, modules[modName])
116120
})
117121

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-
}
122+
const onInitialize = (action) => action.parallel(result.initializers)
126123

127124
return {
128125
onInitialize,

0 commit comments

Comments
 (0)