Trigger a state transition by event.
The event to trigger
Error if FSM not initialized (unless throwOnInvalid is false)
Error if transition is invalid (unless throwOnInvalid is false)
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.
The event to check
true if the transition is valid, false otherwise
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.
The state to check
true if currently in the specified state, false otherwise
if (this.isInState('idle')) {
console.log('System is idle');
}
Current state of the state machine. Observable property that updates automatically via @kosModel. Always initialized to the initial state immediately.
Readonly isIndicates whether the state machine has been initialized. FSM initializes at the configured lifecycle phase (READY, ACTIVATE, etc.).
false, currentState === undefinedtrue, currentState === initialIndicates whether a state transition is currently in progress. Observable property that updates during transitions.
Readonly allowedComputed 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.
Optional stateHistory 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.
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
Lifecycle-Aware Initialization
The FSM is uninitialized until the configured lifecycle phase:
currentState === undefined,isFsmInitialized === falsecurrentState === initial,isFsmInitialized === trueInjected 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 initializedisTransitioning: Boolean indicating if a transition is in progressallowedTransitions: Computed array of valid events for current statetransition(event): Method to trigger state transitionscanTransition(event): Check if a transition is valid from current stateisInState(state): Check if currently in a specific statestateHistory: Array of historical states (if trackHistory enabled)All properties are reactive and will automatically update UI components when state changes.
See