Skip to content

HandlerFn

HandlerFn<TCtx, TStateNames> = (args, …extra) => TStateNames | void

Defined in: types.ts:196

A function handler for state inputs, lifecycle hooks (_onEnter, _onExit), and catch-all (*) handlers.

Return value determines transition:

  • Return a valid state name → FSM transitions to that state
  • Return void/undefined → FSM stays in the current state

This mirrors gen_fsm’s {next_state, StateName, NewStateData} return. Guards are just if statements. Actions are just code before the return.

The ...extra rest parameter captures additional arguments passed through handle(inputName, ...extraArgs). These are untyped (unknown[]) because correlating per-input arg types with handle() call sites would require prohibitively complex mapped types for minimal benefit.

TCtx

TStateNames extends string = string

HandlerArgs<TCtx, TStateNames>

unknown[]

TStateNames | void

// Side effects only, no transition:
tick({ ctx }) { ctx.tickCount++; }

// Conditional transition (replaces guard + target):
timeout({ ctx }) {
  if (ctx.tickCount >= 3) return "yellow";
}

// Unconditional transition with side effect (replaces action + target):
timeout({ ctx }) {
  console.log("transitioning after", ctx.tickCount, "ticks");
  return "yellow";
}

// Handler with extra args passed via handle("success", responseData):
success({ ctx }, data) { ctx.result = data; }