Skip to content

Commit

Permalink
fix: allow async wait state handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed May 5, 2023
1 parent f18ef09 commit 5a79791
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/__tests__/waitState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,53 @@ describe("waitState", () => {
expect(isState(runtime.currentState(), TimedOutState)).toBeTruthy()
expect((runtime.currentState().data as D).count).toBe(INITIAL_COUNT)
})

it("should accept async handlers", async () => {
const BeforeThing = state<Enter, D>(
{
Enter: data => WaitForThing([data, RETURN_COUNT]),
},
{ name: "BeforeThing" },
)

const TimedOutState = state<Enter, D>(
{
Enter: noop,
},
{ name: "TimedOutState" },
)

const WaitForThing = waitState(
fetchThing,
thingFetched,
async (data: D, payload) => {
return AfterThing({ ...data, count: data.count + payload })
},
{
name: "WaitForThing",
timeout: 1000,
onTimeout: async data => {
return TimedOutState(data)
},
},
)
const context = createInitialContext([
BeforeThing({
count: INITIAL_COUNT,
}),
])

const runtime = createRuntime(context, {}, { fetchThing })

expect(isState(runtime.currentState(), BeforeThing)).toBeTruthy()

await runtime.run(enter())

expect(isState(runtime.currentState(), WaitForThing)).toBeTruthy()

await timeout(2000)

expect(isState(runtime.currentState(), TimedOutState)).toBeTruthy()
expect((runtime.currentState().data as D).count).toBe(INITIAL_COUNT)
})
})
4 changes: 2 additions & 2 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ export const waitState = <
>(
requestAction: ReqAC,
responseActionCreator: RespAC,
transition: (data: Data, payload: RespA["payload"]) => StateReturn,
transition: (data: Data, payload: RespA["payload"]) => HandlerReturn,
options?: {
name?: string
timeout?: number
onTimeout?: (data: Data) => StateReturn
onTimeout?: (data: Data) => HandlerReturn
},
) => {
const name = options?.name
Expand Down

0 comments on commit 5a79791

Please sign in to comment.