Stately
XState v6 alpha

Inspection

Observe actor systems and transitions.

XState v6 is in alpha

APIs and behavior may change before the stable release.

Pass an inspector to the root actor with the inspect option.

const actor = createActor(machine, {
  inspect: (inspectionEvent) => {
    console.log(inspectionEvent.type, inspectionEvent);
  }
});

Inspection emits two event types:

TypeContains
@xstate.actorActor identity and parent information.
@xstate.transitionThe event, snapshot, source, target and microsteps.

Both event types carry rootId, the session ID of the root actor, and actorRef, the actor the event is about. Session IDs are unique across actors, so rootId identifies the system and actorRef.sessionId identifies an actor within it.

@xstate.actor announces every created actor: the root actor and each spawned or invoked child. It is the only topology event, so an inspector can draw the actor graph before any transition occurs.

PropertyDescription
parentRefThe parent actor, or undefined for the root actor.
idThe actor's id.
srcThe source logic, or its referenced string.
snapshotThe actor's initial snapshot.

@xstate.transition announces a transition. Every property is always present, so an inspector can read each facet without narrowing.

PropertyDescription
eventTypeThe event type.
eventThe event that caused the transition.
sourceRefThe actor that sent the event, if any.
targetRefThe target actor, usually the same as actorRef.
snapshotThe resulting snapshot.
microstepsThe microstep transition definitions taken.
actionsThe executed actions, as { type, params }.
sentEvents relayed to other actors, as { targetRef, targetId, event, delay, id }.

Actor stop is derivable from snapshot.status on the actor's final @xstate.transition event, so there is no separate stop event. The v5 @xstate.event, @xstate.snapshot, @xstate.action and @xstate.microstep events are gone; @xstate.transition carries all of them.

Use inspection for developer tools, logs and visualizers. Do not change application state from an inspector.

Use inspection to build a statechart visualizer, or to attach actor and transition details to a failed request trace.

Inspection cheatsheet

createActor(logic, { inspect });
createActor(logic, { inspect: (event) => console.log(event) });
event.rootId; // root actor session ID
event.actorRef; // '@xstate.actor' and '@xstate.transition'
event.parentRef, event.id, event.src, event.snapshot; // '@xstate.actor'
event.event, event.snapshot, event.sourceRef, event.targetRef; // '@xstate.transition'
event.microsteps, event.actions, event.sent; // '@xstate.transition'

On this page