Skip to content

Examples Overview

machina ships with eight runnable examples covering real-world patterns — from async health checks to React integration to test suite patterns. Each one is a self-contained app you can run locally, and on the deployed docs site they’re live at their own routes.

ExampleWhat it demonstratesKey machina features
Connectivity MonitorNetwork connectivity tracking with async health checkscreateFsm, async in _onEnter, AbortController cleanup in _onExit, custom events
Traffic IntersectionHierarchical traffic light controller with pedestrian signals_child delegation, compositeState(), input bubbling, defer(), child auto-reset
Dungeon CrittersMultiple critter instances sharing one FSM definitioncreateBehavioralFsm, per-client state via WeakMap, shared behavior definition
Shopping CartCheckout flow with deferred operationscreateFsm, defer(), defer({ until }), lifecycle hooks
Job QueuePersist/restore pattern with rehydrate() and localStoragecreateBehavioralFsm, rehydrate(), silent state restoration, side-effect recovery
With ReactReact integration for a todo appcreateFsm, useSyncExternalStore, framework integration
machina-explorerInteractive FSM inspector and state diagram visualizerbuildStateGraph, inspectGraph, mermaid stateDiagram-v2, CodeMirror 6
Testing with machina-testGraph matchers, hierarchical FSM testing, and walkAll runtime testingmachina-test matchers, walkAll, payload generators, seed replay

On the deployed docs site, each example is a working app at /demos/{name}/. The doc pages above explain the patterns; the “Run” link at the top of each page opens the live app in a new tab.

During local development, examples run on their own Vite dev server — the docs site doesn’t serve them. See below.

# Run any example's dev server
pnpm --filter @machina-examples/connectivity dev

# Or from the example directory
cd examples/connectivity && pnpm dev

Swap connectivity for any of the example names: traffic-intersection, dungeon-critters, shopping-cart, job-queue, with-react, machina-explorer, or testing-with-machina-test.

The testing-with-machina-test example is test-only (no Vite dev server) — run it with pnpm --filter @machina-examples/testing-with-machina-test test.