xstate icon indicating copy to clipboard operation
xstate copied to clipboard

[v5] `stop()` for multiple actors

Open davidkpiano opened this issue 2 years ago • 4 comments

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:

  • https://discord.com/channels/795785288994652170/1160570997209051256

davidkpiano avatar Oct 08 '23 14:10 davidkpiano

🦋 Changeset detected

Latest commit: a5d1e82105371b9f41517305310e9fe2f18539ce

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

changeset-bot[bot] avatar Oct 08 '23 14:10 changeset-bot[bot]

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.

codesandbox-ci[bot] avatar Oct 08 '23 14:10 codesandbox-ci[bot]

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.

Andarist avatar Oct 08 '23 16:10 Andarist

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 👍

davidkpiano avatar Oct 08 '23 17:10 davidkpiano

🧹 Closing as this can be done using enqueueActions(…):

actions: enqueueActions(x => {
  x.context.actors.forEach(actor => x.enqueue.stop(actor));
})

davidkpiano avatar Jul 05 '24 19:07 davidkpiano