Introduction
What is machina?
Section titled “What is machina?”machina is a focused library for building finite state machines in JavaScript and TypeScript. You define states and the inputs each state responds to, then write handlers for those inputs. A handler returns a state name to transition, or returns nothing to stay put.
The library provides two factory functions: createFsm and createBehavioralFsm. createFsm produces a single-client FSM with its own context (data) object. Use it when you have one instance of a thing. createBehavioralFsm produces a multi-client FSM that tracks state per client in a WeakMap. One FSM definition serves any number of independent clients, and machina doesn’t write properties onto your objects. Both produce FSMs with the same core API.
Handlers are plain functions (or string shorthand for simple transitions) that receive a { ctx, inputName, defer, emit } args object. Lifecycle hooks (_onEnter, _onExit) run on entry and exit of each state. Child FSMs attach to parent states via _child for hierarchical behavior, with unhandled inputs bubbling up. When a handler can’t deal with an input yet, defer() re-queues it for replay after the next transition.
Design philosophy
Section titled “Design philosophy”machina was originally inspired by the gen_fsm behavior module from Erlang/OTP. JavaScript runtimes differ from Erlang in almost every mechanical detail, but the same goals apply: pragmatic, focused on FSM patterns, minimal ceremony, and straightforward to reason about.
machina is TypeScript-first. State names, input names, transition targets, and handler signatures are all inferred from the config object, and createFsm doesn’t need manual type parameters. String shorthand targets (timeout: "red") are validated against actual state keys at compile time, so a typo is a type error. An opt-in input payload map extends the checking to payloads: declare each input’s argument tuple once, and the compiler checks handlers and every handle() call against it.
How machina differs from other libraries
Section titled “How machina differs from other libraries”The most prominent alternative is XState. XState is a full statechart framework with actors, spawning, inspection tools, and SCXML compatibility. It’s a phenomenal library, and if you need those features, use it.
machina is for when you want a state machine (or a hierarchy of them) and nothing else: states, inputs, transitions, and the occasional child FSM. It’s intentionally built without opinions on how you manage adjacent concerns, so you can fit it neatly into the slot where you need FSMs to help tame complexity.
Companion tools
Section titled “Companion tools”The companion packages cover static analysis, testing, linting, and React:
- machina-inspect: static analysis that parses your config into a graph IR and runs structural checks (unreachable states,
_onEnterloops, missing handlers). Use it programmatically, or as the engine behind machina-test and the ESLint plugin. - machina-test: Jest and Vitest matchers for graph assertions, plus property-based runtime testing with
walkAll. - eslint-plugin-machina: the machina-inspect checks inline in your editor via ESLint.
- machina-react: React hooks that subscribe components to machina FSMs. It is lightweight and experimental.
- machina-explorer: a browser-based paste-and-analyze tool that runs the checks and renders a mermaid state diagram. It runs entirely in the browser, and you can try it live.
Next steps
Section titled “Next steps”Ready to build? Head to Getting Started. If you are coming from v7, the v7 to v8 guide covers v8’s one type-level break. If you are coming from v5 or earlier, read the v5 to v6 guide and the v6 to v7 guide before it.