Interface KosStateMachineConfig<TState, TEvent>

Configuration for a KOS state machine.

Example

const config: KosStateMachineConfig<'idle' | 'active', 'START' | 'STOP'> = {
initial: 'idle',
initializeAt: DependencyLifecycle.READY,
states: {
idle: { on: { START: 'active' } },
active: { on: { STOP: 'idle' } }
}
};
interface KosStateMachineConfig<TState, TEvent> {
    initial: TState;
    states: Record<TState, {
        on?: Partial<Record<TEvent, TState>>;
    }>;
    initializeAt?: DependencyLifecycle;
}

Type Parameters

  • TState extends string

    Union type of valid state strings

  • TEvent extends string

    Union type of valid event strings

Properties

initial: TState

The initial state when the state machine is initialized. This state will be set when the FSM reaches its configured lifecycle phase.

states: Record<TState, {
    on?: Partial<Record<TEvent, TState>>;
}>

State machine definition mapping states to their allowed transitions.

Type declaration

  • Optional on?: Partial<Record<TEvent, TState>>

    Mapping of events to target states. When an event occurs in this state, transition to the target state.

Example

states: {
idle: { on: { PREPARE: 'preparing' } },
preparing: { on: { READY: 'ready', ERROR: 'error' } },
ready: { on: { POUR: 'pouring' } },
pouring: { on: { COMPLETE: 'idle', ERROR: 'error' } },
error: { on: { RESET: 'idle' } }
}
initializeAt?: DependencyLifecycle

The lifecycle phase at which the state machine should be initialized. Before this phase, currentState will be undefined and isFsmInitialized will be false.

Default

DependencyLifecycle.READY

Example

// FSM initializes when model is ready (most common)
initializeAt: DependencyLifecycle.READY

// FSM initializes only when UI activates the model
initializeAt: DependencyLifecycle.ACTIVATE

// FSM initializes during model initialization (rare)
initializeAt: DependencyLifecycle.INIT