Skip to content

Hierarchical States

Any state can mount a child FSM by setting _child to an FSM instance. When the parent occupies that state, handle() checks the child first through canHandle(), which looks through the child’s entire _child chain. If anything down the chain can handle the input, machina forwards it. Otherwise the parent’s own state handlers get it. A child that sends itself an input it can’t handle (e.g. from _onEnter) fires nohandler and bubbles the input up to the parent.

Assign any createFsm (or createBehavioralFsm) instance to the _child property of a state definition. The parent delegates to it automatically, without any other wiring.

import { createFsm } from "machina";

const childFsm = createFsm({
    id: "upload-phases",
    initialState: "preparing",
    context: {},
    states: {
        preparing: { ready: "uploading" },
        uploading: { done: "verifying" },
        verifying: { verified: "complete" },
        complete: {},
    },
});

const uploader = createFsm({
    id: "uploader",
    initialState: "idle",
    context: {},
    states: {
        idle: {
            start: "active",
        },
        active: {
            _child: childFsm, // inputs go here first
            cancel: "idle", // "cancel" isn't on childFsm, so it bubbles here
        },
    },
});

When handle() is called on the parent, the dispatch order is:

  1. Input arrives at the parent via handle()
  2. Parent checks if the current state has a _child
  3. If yes, the parent calls canHandle() on the child, which answers for the child’s whole _child chain. A grandchild’s input counts.
  4. If anything down the chain can handle it, the child’s handle() receives the input and repeats the same dispatch one level down. The forwarding stops at the FSM that handles the input.
  5. If nothing down the chain can handle it, the parent’s own state handlers get the input

The descendant chain has precedence, and the parent’s handlers act as a fallback, with the same child-first order at every level. Delegation goes as deep as your hierarchy nests, to the same depth as compositeState(). An input handled three levels down can be dispatched from the root without manual forwarding. Separately, a child that sends itself an input it can’t handle (e.g. inside _onEnter) fires nohandler and bubbles the input up to the parent. The traffic intersection’s phaseComplete input takes this path from the child’s red state to the parent.

The nohandler-bubbling mechanism above works whether or not the child says anything about it. Without a declaration, though, the compiler cannot warn that a container needs to catch the bubble. bubbles adds that declaration: a config property listing the inputs an FSM fires at itself without handling. TypeScript can then enforce that whatever mounts the FSM via _child actually deals with them. Under an input payload map, bubble entries must also be keys of the map: a bubble the FSM can never fire is rejected at the declaration.

// `fsm` is declared before createFsm() and assigned after, so _onEnter can
// close over it safely — the same pattern the traffic intersection example
// uses (setTimeout runs well after createFsm() returns and the assignment
// completes).
let fsm: ReturnType<typeof createFsm>;

const phaseController = createFsm({
    id: "phase-controller",
    initialState: "green",
    bubbles: ["phaseComplete"], // "I fire this; whoever mounts me must deal with it"
    states: {
        green: { advance: "red" },
        red: {
            _onEnter() {
                // Not handled here — this is exactly the self-directed
                // dispatch the nohandler-bubbling mechanism above describes.
                setTimeout(() => fsm.handle("phaseComplete"), 0);
            },
        },
    },
});

fsm = phaseController;

Declaring a bubble does two things:

  1. It joins the FSM’s own typed input union, so the self-directed fsm.handle("phaseComplete") call above type-checks without a cast.
  2. It becomes part of the FSM’s mounting contract. Any config that mounts phaseController via _child must satisfy it. Otherwise the mount is a compile error:
// Compile error on `_child` — "phaseComplete" is declared but neither
// handled here, re-declared, nor covered by a catch-all.
const intersection = createFsm({
    id: "intersection",
    initialState: "northSouth",
    states: {
        northSouth: { _child: phaseController },
        clearance: { advance: "northSouth" },
    },
});

Fixing it means doing one of three things:

// 1. Handle it directly — anywhere in the config, not necessarily on the
//    same state that declares `_child`. Coverage is FSM-wide because a
//    bubble re-dispatches against whatever state the parent is in when it
//    fires, which can be any state.
const intersection = createFsm({
    id: "intersection",
    initialState: "northSouth",
    states: {
        northSouth: {
            _child: phaseController,
            phaseComplete: "clearance",
        },
        clearance: { advance: "northSouth" },
    },
});

// 2. Re-declare it in your OWN `bubbles` — this discharges the mount but
//    passes the obligation up one level: whatever mounts `intersection`
//    now owes `phaseComplete` too.
const intersection2 = createFsm({
    id: "intersection-passthrough",
    initialState: "northSouth",
    bubbles: ["phaseComplete"],
    states: {
        northSouth: { _child: phaseController },
        clearance: { advance: "northSouth" },
    },
});

// 3. Carry a "*" catch-all anywhere in the config — it absorbs every
//    bubble along with everything else.
const intersection3 = createFsm({
    id: "intersection-catchall",
    initialState: "northSouth",
    states: {
        northSouth: {
            _child: phaseController,
            "*"({ inputName }) {
                console.log("unhandled:", inputName);
            },
        },
        clearance: { advance: "northSouth" },
    },
});

The obligation repeats at each level. A grandparent that mounts a re-declaring parent must also cover the bubble, and so on up the chain. An FSM without a bubbles declaration (the default) mounts anywhere.

See the Traffic Intersection example for bubbles in a real hierarchy: the phase controller declares bubbles: ["phaseComplete"], and the intersection parent covers it with a handler on each phase state.

compositeState() returns the full state path as a dot-delimited string, walking down through active child FSMs. If the parent is in "active" and the child is in "uploading", you get "active.uploading".

uploader.handle("start");
uploader.compositeState(); // "active.preparing"

uploader.handle("ready");
uploader.compositeState(); // "active.uploading"

uploader.handle("done");
uploader.compositeState(); // "active.verifying"

This is useful for driving UIs: one compositeState() call reports the whole path, without a query to each FSM.

Nesting is unbounded. A child can itself have a _child, and compositeState() walks the whole chain: "stateA.stateB.stateC".

When the parent transitions into a state that owns _child, machina automatically calls reset() on the child, returning it to its initialState. This happens after _onEnter and the transitioned event, but before deferred queue processing.

uploader.handle("start");
uploader.compositeState(); // "active.preparing"

uploader.handle("ready");
uploader.compositeState(); // "active.uploading"

// Cancel drops back to idle, then...
uploader.handle("cancel");
uploader.compositeState(); // "idle"

// Re-entering "active" auto-resets childFsm back to "preparing"
uploader.handle("start");
uploader.compositeState(); // "active.preparing", fresh start

Re-entering a parent state always starts the child fresh. To restore a client at a specific point in a child hierarchy without triggering lifecycle hooks, use rehydrate() with a composite dot-path, e.g. fsm.rehydrate(client, "active.uploading").

dispose() on the parent cascades to child FSMs by default. If the same child FSM appears in multiple states, it is only disposed once. The parent’s onDispose hook runs before the cascade, and each child’s own hook runs inside that child’s dispose(). A child that throws during the cascade is collected into the parent’s AggregateError along with any hook failures, and the remaining children still dispose.

uploader.dispose();
// childFsm is also disposed: all method calls become silent no-ops

Pass { preserveChildren: true } to skip child disposal and keep the child FSM running independently:

uploader.dispose({ preserveChildren: true });
// childFsm is still alive

The uploader above, assembled and exercised:

import { createFsm } from "machina";

const childFsm = createFsm({
    id: "upload-phases",
    initialState: "preparing",
    context: {},
    states: {
        preparing: { ready: "uploading" },
        uploading: { done: "verifying" },
        verifying: { verified: "complete" },
        complete: {},
    },
});

const uploader = createFsm({
    id: "uploader",
    initialState: "idle",
    context: {},
    states: {
        idle: {
            start: "active",
        },
        active: {
            _child: childFsm,
            cancel: "idle",
        },
    },
});

uploader.handle("start");
uploader.compositeState(); // "active.preparing"

uploader.handle("ready");
uploader.compositeState(); // "active.uploading"

uploader.handle("done");
uploader.compositeState(); // "active.verifying"

uploader.handle("verified");
uploader.compositeState(); // "active.complete"

// "cancel" has no handler on childFsm, bubbles to parent
uploader.handle("cancel");
uploader.compositeState(); // "idle"

// Re-start: child resets automatically to "preparing"
uploader.handle("start");
uploader.compositeState(); // "active.preparing"

For a more complex real-world case (two independent child FSM instances, input bubbling across phases, defer() for pedestrian requests, and child auto-reset driving a full traffic signal cycle), see the Traffic Intersection example.