Machine
Machine:
object
Defined in: Core/Machine.ts:180
Type Declaration
Section titled “Type Declaration”interpret
Section titled “interpret”interpret: <
States,Events,BusEvents>(machine,options) =>MachineActor<States,Events>
Starts an actor that stores state and runs its entry and exit effects.
Manages active state lifecycles:
- Runs
entryeffects on state entry, providing anAbortSignaland guardedsend. - Automatically aborts active controllers and executes cleanup disposers when exiting a state.
- Suppresses stale async dispatches from previously active states.
- Queues re-entrant
sendcalls to preserve sequential transition semantics. - Optionally synchronizes with an EventBus.
- Propagates synchronous callback errors after completing the active transition or cleanup.
Type Parameters
Section titled “Type Parameters”States
Section titled “States”States extends WithKind<string> & object
Events
Section titled “Events”Events extends Record<string, unknown>
BusEvents
Section titled “BusEvents”BusEvents extends Record<string, unknown> = Events
Parameters
Section titled “Parameters”machine
Section titled “machine”Machine<States, Events>
options
Section titled “options”MachineInterpretOptions<States, Events, BusEvents> & CompatibleBus<Events, BusEvents>
Returns
Section titled “Returns”MachineActor<States, Events>
Machine.transition for pure stateless transitions.
Example
Section titled “Example”is: <
K>(kind) => <S>(state) =>state is Extract<S, { kind: K }>
Curried type guard that refines a state to a specific variant kind.
Type Parameters
Section titled “Type Parameters”K extends string
Parameters
Section titled “Parameters”K
Returns
Section titled “Returns”<S>(state) => state is Extract<S, { kind: K }>
Example
Section titled “Example”make: <
States,Events>(states) =>Machine<States,Events>
Creates a state machine definition from state configurations and event handlers.
Type Parameters
Section titled “Type Parameters”States
Section titled “States”States extends WithKind<string> & object
Events
Section titled “Events”Events extends Record<string, unknown>
Parameters
Section titled “Parameters”states
Section titled “states”MachineTransitionTable<States, Events>
Returns
Section titled “Returns”Machine<States, Events>
Example
Section titled “Example”match: {<
States,R>(cases): (state) =>R; <States,Cases>(state,cases):HandlerReturn<Cases[keyofCases]>; } =matchMachine
Exhaustive pattern matcher across all state variants of a machine.
The data-first form infers state payloads and the union of branch return values. The curried
form remains available for pipe, but requires explicit state and return type arguments.
Call Signature
Section titled “Call Signature”<
States,R>(cases): (state) =>R
Type Parameters
Section titled “Type Parameters”States
Section titled “States”States extends WithKind<string> & object
R
Parameters
Section titled “Parameters”{ readonly [K in string]: (state: Extract<States, { kind: K }>) => R }
Returns
Section titled “Returns”(state) => R
Call Signature
Section titled “Call Signature”<
States,Cases>(state,cases):HandlerReturn<Cases[keyofCases]>
Type Parameters
Section titled “Type Parameters”States
Section titled “States”States extends WithKind<string> & object
Cases extends MachineMatchCases<States>
Parameters
Section titled “Parameters”States
Cases
Returns
Section titled “Returns”HandlerReturn<Cases[keyof Cases]>
Example
Section titled “Example”state: {<
Kind>(kind):WithKind<Kind> &object; <Kind,Data>(kind,value):MachineState<Kind,Data,string>; } =makeState
Constructs a state variant without requiring manual type assertions.
Call Signature
Section titled “Call Signature”<
Kind>(kind):WithKind<Kind> &object
Type Parameters
Section titled “Type Parameters”Kind extends string
Parameters
Section titled “Parameters”Kind
Returns
Section titled “Returns”WithKind<Kind> & object
Call Signature
Section titled “Call Signature”<
Kind,Data>(kind,value):MachineState<Kind,Data,string>
Type Parameters
Section titled “Type Parameters”Kind extends string
Data
Parameters
Section titled “Parameters”Kind
Data
Returns
Section titled “Returns”MachineState<Kind, Data, string>
Example
Section titled “Example”to:
object
Converters to standard external runtime structures.
to.reducer
Section titled “to.reducer”reducer: <
States,Events>(machine) => (state,event) =>States
Converts a state machine into an uncurried reducer function (state, event) => nextState
directly compatible with React useReducer.
Type Parameters
Section titled “Type Parameters”States
Section titled “States”States extends WithKind<string> & object
Events
Section titled “Events”Events extends Record<string, unknown>
Parameters
Section titled “Parameters”machine
Section titled “machine”Machine<States, Events>
Returns
Section titled “Returns”(state, event) => States
Machine.transition for pure, curried data-last pipeline transitions.
Example
Section titled “Example”transition
Section titled “transition”transition: <
States,Events>(machine,event) => (state) =>States
Pure, data-last transition function. Evaluates a transition on the provided state and returns the next state without executing lifecycle side effects.
If the state does not handle the event, the current state is returned unchanged. State kinds and event kinds must be declared machine members; external data must be validated before it enters a machine (garbage-in-garbage-out — a prototype-named kind through a contract violation is treated as an inherited property, not a member).
Type Parameters
Section titled “Type Parameters”States
Section titled “States”States extends WithKind<string> & object
Events
Section titled “Events”Events extends Record<string, unknown>
Parameters
Section titled “Parameters”machine
Section titled “machine”Machine<States, Events>
MachineEventMessage<Events>
Returns
Section titled “Returns”(state) => States
Machine.interpret to run a stateful actor with async lifecycle effects.