Skip to content

Conversation

davidkpiano
Copy link
Member

Added onStop handler to actors which allows registering callbacks that will be executed when an actor is stopped. This provides a way to perform cleanup or trigger side effects when an actor reaches its final state or is explicitly stopped.

const actor = createActor(someMachine);

actor.onStop(() => {
  console.log('Actor stopped');
});

@davidkpiano davidkpiano requested a review from Andarist April 29, 2025 12:21
Copy link

changeset-bot bot commented Apr 29, 2025

🦋 Changeset detected

Latest commit: daf332d

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

This PR includes changesets to release 6 packages
Name Type
xstate Minor
@xstate/graph Major
@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

@tnortman-jabra
Copy link

tnortman-jabra commented Apr 29, 2025

Any way for there to be some way to handle this in the state machine configuration itself?

For example:

setup({
  actions: {
    criticalAction: () => { 
       // iWantToRunOnTeardownNoMatterWhat 
    },
  },
}).createMachine({
  stop: 'criticalAction',
});

@Andarist
Copy link
Collaborator

Andarist commented May 4, 2025

@tnortman-jabra it already exists (sort of):

import { setup, fromCallback } from "xstate";

setup({
  actors: {
    criticalCleanup: fromCallback(() => () => {
      // do stuff
    }),
  },
}).createMachine({
  invoke: {
    src: "criticalCleanup",
  },
});

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