Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .changeset/async-step-store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
'@xstate/store': minor
---

Add async step support to `createStore(...)` with `await enq.step(stepId, exec)`.

```ts
const store = createStore({
context: {
result: undefined as number | undefined
},
on: {
load: async (context, event: { id: string }, enq) => {
const result = await enq.step('fetchResult', () => fetchResult(event.id));

return {
...context,
result
};
}
}
});
```

This keeps `@xstate/store` aligned with its state-machine-like model: pending
async work is captured in the snapshot, and step completion is handled through
explicit followup events. That means an in-progress async transition can be
replayed deterministically from persisted snapshot state instead of depending on
an opaque promise continuation.
3 changes: 3 additions & 0 deletions packages/xstate-store/src/fromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export function fromStore<
typeof initialContext === 'function'
? initialContext(input)
: initialContext,
async: {
pending: {}
},
output: undefined,
error: undefined
} satisfies StoreSnapshot<TContext>;
Expand Down
Loading
Loading