statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
26.51k stars 1.22k forks source link

[core] Inspect subscription #4936

Closed davidkpiano closed 2 weeks ago

davidkpiano commented 3 weeks ago

Inspecting an actor system via actor.system.inspect(ev => …) now accepts a function or observer, and returns a subscription:

const actor = createActor(someMachine);

const sub = actor.system.inspect((inspectionEvent) => {
  console.log(inspectionEvent);
});

// Inspection events will be logged
actor.start();
actor.send({ type: 'anEvent' });

// ...

sub.unsubscribe();

// Will no longer log inspection events
actor.send({ type: 'someEvent' });
changeset-bot[bot] commented 3 weeks ago

🦋 Changeset detected

Latest commit: 2ee203299f09aa4b2f23da65ed885e290911024b

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

boneskull commented 3 weeks ago

The subscription is welcome, but this also makes it easier to attach an inspector on-the-fly, because you don't need to use toObserver to do so.