State machine configuration
Optional options: KosStateMachineOptionsOptional configuration for FSM behavior
A class decorator that modifies the target class prototype
reactive system
and
decorators
decorator
Framework Lifecycle (Universal, Not Customizable):
Application FSM (This Decorator):
The FSM initializes at the configured lifecycle phase:
@kosStateMachine({
initial: 'idle',
initializeAt: DependencyLifecycle.READY, // Default
states: { ... }
})
IMPORTANT: Use TypeScript interface merging to get proper type information:
type MyState = 'idle' | 'active';
type MyEvent = 'START' | 'STOP';
interface MyModelImpl extends KosStateMachineAware<MyState, MyEvent> {}
@kosStateMachine<MyState, MyEvent>({ ... })
class MyModelImpl implements IKosDataModel {
// All FSM properties are now available with full type safety
}
// Basic FSM with default READY initialization
type PourState = 'idle' | 'pouring' | 'complete';
type PourEvent = 'START' | 'FINISH';
interface PourModelImpl extends KosStateMachineAware<PourState, PourEvent> {}
@kosModel({ modelTypeId: "pour-model" })
@kosStateMachine<PourState, PourEvent>({
initial: 'idle',
states: {
idle: { on: { START: 'pouring' } },
pouring: { on: { FINISH: 'complete' } },
complete: {}
}
})
class PourModelImpl implements IKosDataModel {
async ready(): Promise<void> {
// After this completes, FSM is initialized to 'idle'
}
startPour(): void {
this.transition('START'); // idle -> pouring
}
}
// UI-bound FSM with ACTIVATE initialization
type WorkflowState = 'draft' | 'review' | 'approved';
type WorkflowEvent = 'SUBMIT' | 'APPROVE' | 'REJECT';
interface DocumentModelImpl extends KosStateMachineAware<WorkflowState, WorkflowEvent> {}
@kosModel({ modelTypeId: "document-model" })
@kosStateMachine<WorkflowState, WorkflowEvent>({
initial: 'draft',
initializeAt: DependencyLifecycle.ACTIVATE, // UI-bound
states: {
draft: { on: { SUBMIT: 'review' } },
review: { on: { APPROVE: 'approved', REJECT: 'draft' } },
approved: {}
}
})
class DocumentModelImpl implements IKosDataModel {
// FSM only initializes when UI activates this model
}
// FSM with history tracking
interface HistoryModelImpl extends KosStateMachineAware<'idle' | 'active', 'TOGGLE'> {}
@kosStateMachine<'idle' | 'active', 'TOGGLE'>(
{
initial: 'idle',
states: {
idle: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'idle' } }
}
},
{
trackHistory: true,
stateProperty: 'status'
}
)
class HistoryModelImpl implements IKosDataModel {
// Access via this.stateHistory
}
2.1.0
Class decorator that adds finite state machine capabilities to a KOS model.
This decorator eliminates manual state management boilerplate by: