FsmConfig
Defined in: types.ts:979
Configuration object for creating an FSM.
Examples
Section titled “Examples”Declaring bubbled inputs for a child FSM
Type Parameters
Section titled “Type Parameters”TCtx
The context type (Fsm) or client type (BehavioralFsm).
For Fsm, this is inferred from the context property. For BehavioralFsm,
it’s the client object type provided explicitly or as a generic parameter.
TStates
Section titled “TStates”TStates extends Record<string, Record<string, unknown>> = Record<string, Record<string, unknown>>
The literal states object type. Captured directly from
the naked states: TStates & ... intersection member below (ideally with
const on the factory’s generic to preserve string literal types) —
deliberately NOT derived from ValidateStates, so an empty or
function-only state can’t disable inference (see ValidateStates’
module comment for the full mechanism). Defaults to a loose record for
unconstrained usage.
TStateNames
Section titled “TStateNames”TStateNames extends string = keyof TStates & string
The state-name union, defaulted from TStates but
captured as its OWN parameter (see ValidateStates) so it’s available,
fully resolved, while handler bodies are still being type-checked.
TBubbles
Section titled “TBubbles”TBubbles extends string = never
The union of inputs this FSM declares via bubbles.
Defaults to never — most FSMs bubble nothing.
TInputs
Section titled “TInputs”TInputs extends InputMapLike<TInputs> = InputMap
The input payload map (#195). The curried factory
forms fix it before inference starts, so it adds nothing to the
inference machinery above. Defaults to the wide map, which keeps
ValidateStates and InputVocabulary behaving exactly as the untyped
path always has.
TEvents
Section titled “TEvents”TEvents extends EventMapLike<TEvents> = EventMap
The event payload map (#183). Fixed by the curried
factory forms the same way as TInputs. Flows into emit on every
handler and on onDispose. Defaults to the wide map.
Properties
Section titled “Properties”bubbles?
Section titled “bubbles?”
optionalbubbles?: readonlyTBubbles[]
Defined in: types.ts:1032
Inputs this FSM fires at itself without handling them — expecting
whatever mounts it via _child to catch them through machina’s
nohandler-bubbling mechanism (see the “Input delegation” section of the
hierarchical states guide). Declaring a bubble does two things:
- Joins this FSM’s own typed input union, so a self-directed
fsm.handle("phaseComplete")(e.g. from inside_onEnter) type-checks without a cast. - Becomes part of this FSM’s mounting contract: any config that
mounts it via
_childmust handle every declared bubble in some state, re-declare it in its OWNbubbles(passing the obligation up another level), or carry a"*"catch-all — enforced at compile time byChildCoverage, which is intersected ontostatesbelow.
A bubble name is NOT a state name — it can’t be used as a string
shorthand transition target. Omit bubbles entirely (the default) for
an FSM that never expects a container to catch anything from it — such
an FSM can be mounted via _child anywhere with no obligations.
context?
Section titled “context?”
optionalcontext?:TCtx
Defined in: types.ts:1010
Initial context data. The type is inferred from this value and flows
into every handler’s ctx parameter.
For BehavioralFsm, this property is optional and serves only as a type constraint — the client object IS the context.
id:
string
Defined in: types.ts:991
Unique identifier for this FSM
initialState
Section titled “initialState”initialState:
NoInfer<TStateNames>
Defined in: types.ts:1001
The state to start in. Must be a key of states.
Wrapped in NoInfer to prevent TypeScript from using this value as an
inference site for TStateNames. Without it, initialState: "green"
could narrow the state-name union to only have a “green” key. We want
inference to come exclusively from the states property.
onDispose?
Section titled “onDispose?”
optionalonDispose?: (args) =>void
Defined in: types.ts:1088
Teardown hook. dispose() calls it once per live client before it
clears listeners and before it disposes child FSMs. An Fsm has one
client, its context, so the hook runs once. A BehavioralFsm runs it
once for every client it has initialized or rehydrated that is still
reachable. A client the garbage collector already reclaimed gets no
call: the FSM tracks clients through WeakRefs by design, and a
resource stored on a collected client is unreachable anyway.
The args object carries ctx and emit only. defer() has no meaning
during teardown, and inputName would always be empty, so the type
omits both. emit() still reaches subscribers because the hook runs
before the emitter clears. handle(), transition(), and reset()
are already no-ops by the time the hook runs.
The hook must tolerate a resource that was already released elsewhere.
A terminal state’s _onEnter can release the same resource before
dispose() runs, and the hook still fires for that client.
The hook is synchronous, like every other FSM method. A request that
is still in flight when dispose() runs cannot be cancelled here: set
a flag on ctx and have the late result check it. A release that
returns a promise outlives dispose(), and dispose() returns before
it settles. Collect such promises outside the FSM and await them after
dispose(). A rejection inside the hook is an unhandled rejection
unless the hook attaches its own catch; AggregateError collects
synchronous throws only.
Resource lifetime decides where cleanup goes. A resource that lives as
long as one state belongs in that state’s _onExit. A resource that
lives as long as the FSM instance, or as long as a client, belongs
here. dispose() does not run _onExit; transition to a terminal
state first when a state-scoped resource must also release on
teardown.
A throwing hook does not stop teardown. dispose() runs every
remaining hook, clears listeners, disposes children, and then throws
one AggregateError that holds every failure.
Parameters
Section titled “Parameters”Pick<HandlerArgs<NoInfer<TCtx>, string, TEvents>, "ctx" | "emit">
Returns
Section titled “Returns”void
Example
Section titled “Example”states
Section titled “states”states:
TStates&ValidateStates<TCtx,TStates,TStateNames,TInputs,TEvents> &ChildCoverage<TStates,TStateNames,TBubbles> &InputVocabulary<TStates,TStateNames,TInputs>
Defined in: types.ts:1099
State definitions. Keys become the state name union.
The intersection with the bare TStates is what makes literal-type
capture work for empty and function-only states (see ValidateStates’
module comment) — ValidateStates and ChildCoverage layer validation
and the bubble-coverage contract on top without becoming the inference
source themselves.