///
interface StateTransition {
from: TState;
to: TState;
guard?: () => boolean;
}
interface StateTransitionAction {
targetState: TState;
action: () => Promise;
onSuccess?: TState;
onError?: TState;
}
/**
* Generic async state machine that can be used to manage state transitions
* with support for async operations, event listeners, and validation.
* All state transitions are queued and processed sequentially to prevent race conditions.
*/
declare class AsyncStateMachine {
private _currentState;
private _initialState;
private _listeners;
private _pendingTransitions;
private _validTransitions;
private _taskQueue;
private _isProcessingQueue;
constructor(initialState: TState, validTransitions: StateTransition[]);
get currentState(): TState;
get isInitializing(): boolean;
addListener(listener: (currentState: TState, previousState: TState, context?: any) => void): void;
removeListener(listener: (currentState: TState, previousState: TState, context?: any) => void): void;
canTransitionTo(targetState: TState): boolean;
transitionTo(targetState: TState, context?: any): void;
/**
* Enqueues a task to be executed sequentially.
* This ensures that all tasks are processed one at a time, preventing race conditions.
*/
private enqueueTask;
/**
* Processes the task queue sequentially.
* Only one task is processed at a time to prevent race conditions.
*/
private processQueue;
executeAsyncTransition(config: StateTransitionAction): Promise;
waitForState(targetState: TState, timeoutMs?: number): Promise;
reset(initialState: TState): Promise;
private notifyListeners;
}
export { AsyncStateMachine, type StateTransition, type StateTransitionAction };