Interface KosStateMachineAware<TState, TEvent>

Interface for models using the

Kos State Machine

decorator.

IMPORTANT: Use TypeScript interface merging to add FSM properties to your model class. This provides full type safety and IntelliSense support for all injected FSM functionality.

Interface Merging Pattern

// 1. Define state and event types
type PourState = 'idle' | 'preparing' | 'ready' | 'pouring' | 'error';
type PourEvent = 'PREPARE' | 'READY' | 'POUR' | 'COMPLETE' | 'ERROR';

// 2. Declare interface merging BEFORE the class
interface DispenserModelImpl extends KosStateMachineAware<PourState, PourEvent> {}

// 3. Apply decorators and implement the class
@kosModel({ modelTypeId: "dispenser-model" })
@kosStateMachine<PourState, PourEvent>({
initial: 'idle',
initializeAt: DependencyLifecycle.READY,
states: {
idle: { on: { PREPARE: 'preparing' } },
preparing: { on: { READY: 'ready', ERROR: 'error' } },
ready: { on: { POUR: 'pouring' } },
pouring: { on: { COMPLETE: 'idle', ERROR: 'error' } },
error: {}
}
})
export class DispenserModelImpl implements IKosDataModel {
// All FSM properties are now available with full type safety
}

Lifecycle-Aware Initialization

The FSM is uninitialized until the configured lifecycle phase:

  • Before initialization: currentState === undefined, isFsmInitialized === false
  • After initialization: currentState === initial, isFsmInitialized === true
@kosStateMachine({
initial: 'idle',
initializeAt: DependencyLifecycle.READY, // FSM starts when model is ready
states: { ... }
})
class MyModel {
async ready(): Promise<void> {
// Before this completes: this.currentState === undefined
// After this completes: this.currentState === 'idle'
}
}

Injected Properties and Methods

The decorator automatically injects the following members:

  • currentState: Current state (undefined until FSM initialized, observable)
  • isFsmInitialized: Boolean indicating if FSM has been initialized
  • isTransitioning: Boolean indicating if a transition is in progress
  • allowedTransitions: Computed array of valid events for current state
  • transition(event): Method to trigger state transitions
  • canTransition(event): Check if a transition is valid from current state
  • isInState(state): Check if currently in a specific state
  • stateHistory: Array of historical states (if trackHistory enabled)

All properties are reactive and will automatically update UI components when state changes.

See

interface KosStateMachineAware<TState, TEvent> {
    currentState: TState;
    isFsmInitialized: boolean;
    isTransitioning: boolean;
    allowedTransitions: TEvent[];
    transition(event): void;
    canTransition(event): boolean;
    isInState(state): boolean;
    stateHistory?: {
        state: TState;
        timestamp: Date;
        event?: TEvent;
    }[];
}

Type Parameters

  • TState extends string

    Union type of valid state strings

  • TEvent extends string

    Union type of valid event strings

Methods

  • Trigger a state transition by event.

    Parameters

    • event: TEvent

      The event to trigger

    Returns void

    Throws

    Error if FSM not initialized (unless throwOnInvalid is false)

    Throws

    Error if transition is invalid (unless throwOnInvalid is false)

    Example

    this.transition('START'); // idle -> active
    this.transition('STOP'); // active -> idle
  • Check if a transition is valid from the current state. Returns false if FSM is not initialized.

    Parameters

    • event: TEvent

      The event to check

    Returns boolean

    true if the transition is valid, false otherwise

    Example

    if (this.canTransition('START')) {
    this.transition('START');
    }
  • Check if the state machine is currently in a specific state. Returns false if FSM is not initialized.

    Parameters

    • state: TState

      The state to check

    Returns boolean

    true if currently in the specified state, false otherwise

    Example

    if (this.isInState('idle')) {
    console.log('System is idle');
    }

Properties

currentState: TState

Current state of the state machine. Observable property that updates automatically via @kosModel. Always initialized to the initial state immediately.

isFsmInitialized: boolean

Indicates whether the state machine has been initialized. FSM initializes at the configured lifecycle phase (READY, ACTIVATE, etc.).

  • Before initialization: false, currentState === undefined
  • After initialization: true, currentState === initial
isTransitioning: boolean

Indicates whether a state transition is currently in progress. Observable property that updates during transitions.

allowedTransitions: TEvent[]

Computed array of valid events that can be triggered from the current state. Updates automatically when state changes. Returns empty array if FSM is not initialized.

stateHistory?: {
    state: TState;
    timestamp: Date;
    event?: TEvent;
}[]

History of state transitions (if trackHistory is enabled). Each entry contains the state, timestamp, and optional event that caused the transition.

Only available when trackHistory: true is set in options.

Type declaration