Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v5] stop() for multiple actors #4343

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

[v5] stop() for multiple actors #4343

wants to merge 5 commits into from

Conversation

davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Oct 8, 2023

You can now stop multiple actors in the stop(...) action creator:

const machine = createMachine({
  types: {
    context: {} as {
      actors: Array<AnyActorRef>;
    }
  },
  context: ({ spawn }) => {
    return {
      actors: [
        spawn(fromPromise(() => Promise.resolve('foo'))),
        spawn(fromPromise(() => Promise.resolve('bar'))),
        spawn(fromPromise(() => Promise.resolve('baz')))
      ]
    };
  },
  on: {
    stopAll: {
      // Return a single actorRef or an array of actorRefs
      actions: stop(({ context }) => context.actors)
    }
  }
});

Discussion:

@changeset-bot
Copy link

changeset-bot bot commented Oct 8, 2023

🦋 Changeset detected

Latest commit: a5d1e82

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

This PR includes changesets to release 1 package
Name Type
xstate Minor

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 8, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@Andarist
Copy link
Member

Andarist commented Oct 8, 2023

I find it slightly weird that this would introduce a lack of symmetry between the proposed spawn action - that action currently doesn't support spawning of multiple actors at the same time. This one here is also a little bit opinionated about the "collection type" of actors - it only supports arrays. It feels to me that usually actor collections are not ordered and for that reason, I'd choose an indexed object over an array to keep them around and get easier/faster access to any particular actor.

Overall, I like the idea - but I have the mentioned concerns. I also think that it's the post-v5 item just because ideally we'd bring support for this to the Studio at the same time and I'd prefer not to work on such improvements before we publish a stable version of v5.

@davidkpiano
Copy link
Member Author

I find it slightly weird that this would introduce a lack of symmetry between the proposed spawn action - that action currently doesn't support spawning of multiple actors at the same time. This one here is also a little bit opinionated about the "collection type" of actors - it only supports arrays. It feels to me that usually actor collections are not ordered and for that reason, I'd choose an indexed object over an array to keep them around and get easier/faster access to any particular actor.

Overall, I like the idea - but I have the mentioned concerns. I also think that it's the post-v5 item just because ideally we'd bring support for this to the Studio at the same time and I'd prefer not to work on such improvements before we publish a stable version of v5.

Agree, this is post-v5 release 👍

@Andarist Andarist changed the base branch from next to main December 1, 2023 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants