Skip to content

Commit bbf6564

Browse files
Merge pull request cerebral#501 from schiller-manuel/wait-for-async-onInitializeOvermind
fix(overmind-statechart): wait for async onInitializeOvermind
2 parents 23c8f66 + 4d193f2 commit bbf6564

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,61 @@ describe('Statecharts', () => {
172172
expect(instance.state.count).toBe(1)
173173
})
174174

175+
test('async onInitializeOvermind is finished before instance is initialized', async () => {
176+
const onInitializeOvermind = async ({actions}: Context) => {
177+
await new Promise(r=>setTimeout(r, 250));
178+
await actions.increaseCount();
179+
}
180+
const increaseCount = async ({ state }: Context) => {
181+
state.count++;
182+
}
183+
const state = {
184+
count: 0,
185+
}
186+
const actions = {
187+
onInitializeOvermind,
188+
increaseCount
189+
}
190+
191+
const config = {
192+
state,
193+
actions,
194+
}
195+
196+
const chart: Statechart<
197+
Context,
198+
{
199+
foo: void,
200+
bar: void
201+
}
202+
> = {
203+
initial: 'foo',
204+
states: {
205+
foo: {
206+
on: {
207+
'increaseCount': 'bar'
208+
}
209+
},
210+
bar:{}
211+
}
212+
}
213+
214+
type Context = IContext<{
215+
state: typeof state,
216+
actions: typeof actions
217+
}>
218+
219+
const instance = createOvermind(
220+
statechart(config, {
221+
id1: chart,
222+
}
223+
))
224+
225+
await instance.initialized
226+
expect(instance.state.states).toEqual([['id1', 'bar']])
227+
expect(instance.state.count).toBe(1)
228+
})
229+
175230
test('should run entry action', () => {
176231
const increaseCount = ({ state }: Context) => {
177232
state.count++
@@ -224,7 +279,7 @@ describe('Statecharts', () => {
224279
expect(instance.state.count).toBe(1)
225280
})
226281

227-
test('should run entry action of initial state when onInitializeOvermind is present', () => {
282+
test('should run entry action of initial state when onInitializeOvermind is present', async () => {
228283
const onInitializeOvermind = ({ state }: Context) => {
229284
state.initCount++
230285
}
@@ -268,7 +323,8 @@ describe('Statecharts', () => {
268323
id1: chart,
269324
})
270325
)
271-
326+
327+
await instance.initialized
272328
expect(instance.state.states).toEqual([['id1', 'foo']])
273329
expect(instance.state.actions).toEqual({ increaseCount: false})
274330
expect(

packages/node_modules/overmind-statechart/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ export function statechart<
294294

295295
const initialActions = {
296296
[ACTIONS]: copiedActions,
297-
onInitializeOvermind: ((context, instance) => {
297+
onInitializeOvermind: ( async (context, instance) => {
298298
if (onInitializeOvermindAction) {
299-
onInitializeOvermindAction(context, instance)
299+
await onInitializeOvermindAction(context, instance)
300300
}
301301

302302
currentInstance = instance

0 commit comments

Comments
 (0)