/** * Importing npm packages */ /** * Defining types */ export type ContextUpdater = Partial | ((currentContext: Context) => Partial); export interface ActionResult { nextState: StateName; contextUpdates?: Partial; } export interface FlowStateDefinition = Record> { /** Function to determine the next possible states from the current state and context. */ getNextStates?: (state: FlowState) => StateName[]; /** Marks this state as a final state in the flow. */ isFinal?: boolean; /** Called when leaving the state. Can return false to prevent transition, or partial context updates. */ onLeave?: (context: Context, nextState: StateName) => void | boolean | Partial; /** Called when entering the state. Can return partial context updates. */ onEnter?: (context: Context, prevState: StateName) => void | Partial; /** Action to be performed in this state. Can return the next state to transition to, or an object with nextState and context updates. */ action?: (context: Context) => void | ActionResult; } export interface FlowState = Record> { currentState: StateName; history: StateName[]; context: Context; } export interface FlowDefinition = Record> { name: string; maxDepth?: number; startState: StateNames; states: Record>; } export interface FlowStatus { name: string; currentState: StateNames; availableTransitions: StateNames[]; isComplete: boolean; history: StateNames[]; } /** * Declaring the constants */ export declare class FlowManager = Record> { private readonly definition; private readonly state; private constructor(); static create = Record>(definition: FlowDefinition, initialContext?: Context): FlowManager; static from = Record>(definition: FlowDefinition, stateOrSnapshot: FlowState | string): FlowManager; toSnapshot(): string; getCurrentState(): StateNames; getDefinition(): FlowDefinition; getHistory(): StateNames[]; isComplete(): boolean; getStatus(): FlowStatus; getContext(): Context; updateContext(updates: ContextUpdater): this; getAvailableTransitions(): StateNames[]; peekTransitions(targetState: StateNames): StateNames[]; canTransitionTo(targetState: StateNames): boolean; private executeTransition; transitionTo(nextState: StateNames, contextUpdates?: ContextUpdater): this; private settle; }