Skip to content

createBehavioralFsm

createBehavioralFsm<TClient, TStates>(config): BehavioralFsm<TClient, StateNamesOf<TStates>, InputNamesOf<TStates>>

Defined in: behavioral-fsm.ts:724

Create a behavioral FSM (one definition, many clients) from a config object.

Generic parameters are inferred automatically:

  • TClient must be provided explicitly as a type parameter (it can’t be inferred from the config since no context property exists at the FSM level).
  • TStates is captured with const inference to preserve string literal types, enabling compile-time validation of transition targets and handle() input names.

State names, input names, and all handler signatures derive from TStates.

TClient extends object

TStates extends Record<string, Record<string, unknown>>

FsmConfig<TClient, TStates>

BehavioralFsm<TClient, StateNamesOf<TStates>, InputNamesOf<TStates>>

interface Connection { url: string; retries: number; }

const connFsm = createBehavioralFsm<Connection>({
  id: "connectivity",
  initialState: "disconnected",
  states: {
    disconnected: { connect: "connecting" },
    connecting:   { connected: "online", failed: "disconnected" },
    online:       { disconnect: "disconnected" },
  },
});

const conn = { url: "wss://example.com", retries: 0 };
connFsm.handle(conn, "connect");