Skip to content

Fsm

Defined in: fsm.ts:55

Single-client FSM. Wraps a BehavioralFsm and uses the config’s context object as the implicit client, so callers never pass a client argument.

Prefer createFsm() over constructing this directly — the factory infers all generic parameters from the config object.

All public methods silently no-op after dispose() is called.

TCtx extends object

The context type, inferred from config.context.

TStateNames extends string

String literal union of valid state names.

TInputNames extends string

String literal union of valid input names.

TBubbles extends string = never

String literal union of inputs this FSM declares via bubbles. Type-only — carried so BubblesOfInstance can extract it from a constructed instance; nothing at runtime reads this generic.

TInputs extends InputMapLike<TInputs> = InputMap

The input payload map (#195). Defaults to the wide map, under which handle()’s signature reduces to exactly the untyped behavior. createFsm<TInputs>() passes the user’s map here.

TEvents extends EventMapLike<TEvents> = EventMap

The event payload map (#183). Defaults to the wide map, under which on() accepts any name with an unknown payload and emit() accepts any name and payload. createFsm<{ events: TEvents }>() passes the user’s map here, which keys both by the declared names.

new Fsm<TCtx, TStateNames, TInputNames, TBubbles, TInputs, TEvents>(config): Fsm<TCtx, TStateNames, TInputNames, TBubbles, TInputs, TEvents>

Defined in: fsm.ts:85

FsmConfig<TCtx, Record<string, Record<string, unknown>>>

Fsm<TCtx, TStateNames, TInputNames, TBubbles, TInputs, TEvents>

readonly [MACHINA_TYPE]: "Fsm"

Defined in: fsm.ts:67


readonly context: TCtx

Defined in: fsm.ts:77


readonly id: string

Defined in: fsm.ts:64


readonly initialState: TStateNames

Defined in: fsm.ts:65


readonly states: Record<string, Record<string, unknown>>

Defined in: fsm.ts:71

canHandle(inputName): boolean

Defined in: fsm.ts:153

Returns true if the current state has a handler for inputName (or a catch-all "*" handler), or if the current state’s _child chain can handle it (checked recursively, matching handle()’s delegation reach). Does not trigger initialization or any side effects. Returns false when disposed.

string

boolean


compositeState(): string

Defined in: fsm.ts:196

Returns the current state as a dot-delimited path that includes any active child FSM states (e.g. "active.connecting.retrying"). Returns just the current state name when no child is active.

string


currentState(): TStateNames

Defined in: fsm.ts:175

Returns the current state name. Always defined — Fsm eagerly initializes into initialState during construction.

TStateNames


dispose(options?): void

Defined in: fsm.ts:250

Permanently shut down this FSM. Irreversible — all subsequent method calls become silent no-ops. Runs the config’s onDispose hook with the context, clears all listeners, and cascades disposal to child FSMs (unless preserveChildren is set). A second call is a no-op.

A throwing onDispose hook (or child disposal) does not stop teardown: the delegate rethrows the failures as one AggregateError after its own cleanup, and this wrapper’s listeners clear regardless.

DisposeOptions

void


emit<K>(eventName, …data): void

Defined in: fsm.ts:231

Emit a custom event through the FSM. Built-in lifecycle events are emitted automatically — this is for user-defined events from handlers. Under a declared event map (#183) the name and payload are checked against it. Routes through the BehavioralFsm so all relay paths are consistent. No-ops when disposed.

K extends string

K

…EmitArgs<TEvents[K]>

void


handle(…call): void

Defined in: fsm.ts:133

Dispatch an input to the current state’s handler. If a _child FSM in the current state can handle it, delegation occurs there first; unhandled inputs bubble up to the parent. No-ops silently when disposed.

…InputCall<TInputNames, TInputs>

void


on(eventName, callback): Subscription

Defined in: fsm.ts:212

Subscribe to a built-in lifecycle event, a custom event, or the wildcard.

Named overload: the callback receives the payload only. A built-in name gets its typed payload. A custom name gets the payload type its event map declares, or unknown when the FSM declares no map; under a declared map an undeclared name is a compile error (#183). Wildcard ("*"): receives (eventName, data) for every event. It is listed first because the named overload’s key type is string under the wide map, so "*" would otherwise match it. Returns a no-op Subscription when disposed.

"*"

(eventName, data) => void

Subscription

on<K>(eventName, callback): Subscription

Defined in: fsm.ts:213

Subscribe to a built-in lifecycle event, a custom event, or the wildcard.

Named overload: the callback receives the payload only. A built-in name gets its typed payload. A custom name gets the payload type its event map declares, or unknown when the FSM declares no map; under a declared map an undeclared name is a compile error (#183). Wildcard ("*"): receives (eventName, data) for every event. It is listed first because the named overload’s key type is string under the wide map, so "*" would otherwise match it. Returns a no-op Subscription when disposed.

K extends string

K

(data) => void

Subscription


reset(): void

Defined in: fsm.ts:164

Transition back to initialState, firing _onEnter and lifecycle events as if entering it fresh. No-ops silently when disposed.

void


transition(toState): void

Defined in: fsm.ts:184

Directly transition to toState, firing _onExit, _onEnter, and lifecycle events. Same-state transitions are silently ignored. No-ops when disposed.

TStateNames

void