Skip to content

Conversation

@davidkpiano
Copy link
Member

Make actors async iterable:

const actor = createActor(machine);
actor.start();

for await (const snapshot of actor) {
  console.log(snapshot);
}

@changeset-bot
Copy link

changeset-bot bot commented Oct 26, 2025

🦋 Changeset detected

Latest commit: b2672fa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
xstate Minor
@xstate/react Major
@xstate/solid Major
@xstate/svelte Major
@xstate/vue Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@davidkpiano davidkpiano requested a review from Andarist October 26, 2025 18:49
@davidkpiano davidkpiano changed the title Davidkpiano/async iterator 1 [core] Async iterator Oct 26, 2025
Comment on lines +490 to +493
// Yield the initial snapshot if the actor is already running
if (this._processingStatus === ProcessingStatus.Running) {
yield this.getSnapshot();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this match the behavior of vanilla actorRef.subscribe?

}

while (this._processingStatus !== ProcessingStatus.Stopped) {
yield await new Promise<SnapshotFrom<TLogic>>((resolve, reject) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is neat but I think we should do better in the library context. This resubscribes with each iteration step. It would be better to maintain a single subscription throughout the full lifecycle of the iteration.

expect(snapshots[2].status).toBe('done');
});

it('should handle errors in async iteration', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't test what it claims - this just happens to use events and states named with "error" but in reality this machine completes gracefully through a regular final state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants