/** * Interrupt State Machine * * Lightweight state machine pattern for interrupt handling. * Ensures valid state transitions and prevents deadlocks. * * State Diagram: * ``` * ┌─────────────┐ * │ idle │ * └──────┬──────┘ * │ * ┌──────────────┼──────────────┐ * │ submit │ cancel │ * ▼ │ ▼ * ┌─────────────┐ │ ┌─────────────┐ * │ submitting │ │ │ cancelled │ * └──────┬──────┘ │ └─────────────┘ * │ │ * ┌──────┴──────┐ │ * │ │ │ * ▼ success ▼ fail │ * ┌─────────┐ ┌─────────┐ │ * │resolved │ │ error │──┘ cancel * └─────────┘ └────┬────┘ * │ retry * ▼ * ┌─────────────┐ * │ submitting │ * └─────────────┘ * ``` * * @module types/interruptState */ /** * Idle state - waiting for user action */ export interface IdleState { readonly status: 'idle'; } /** * Submitting state - API call in progress */ export interface SubmittingState { readonly status: 'submitting'; /** The value being submitted */ readonly pendingValue: unknown; /** Whether this is a cancel operation */ readonly isCancelAction: boolean; } /** * Resolved state - successfully completed */ export interface ResolvedState { readonly status: 'resolved'; /** The submitted value */ readonly value: unknown; /** Timestamp when resolved */ readonly resolvedAt: string; } /** * Cancelled state - user cancelled */ export interface CancelledState { readonly status: 'cancelled'; /** Timestamp when cancelled */ readonly cancelledAt: string; } /** * Error state - submission failed, can retry */ export interface ErrorState { readonly status: 'error'; /** The error message */ readonly error: string; /** The value that failed to submit (for retry) */ readonly failedValue?: unknown; /** Whether the failed action was a cancel */ readonly wasCancelAction: boolean; } /** * Union of all possible interrupt states */ export type InterruptState = IdleState | SubmittingState | ResolvedState | CancelledState | ErrorState; /** * Submit action - user submits a value */ export interface SubmitAction { readonly type: 'SUBMIT'; readonly value: unknown; } /** * Cancel action - user cancels the interrupt */ export interface CancelAction { readonly type: 'CANCEL'; } /** * Success action - API call succeeded */ export interface SuccessAction { readonly type: 'SUCCESS'; } /** * Failure action - API call failed */ export interface FailureAction { readonly type: 'FAILURE'; readonly error: string; } /** * Retry action - retry after error */ export interface RetryAction { readonly type: 'RETRY'; } /** * Reset action - reset to idle state */ export interface ResetAction { readonly type: 'RESET'; } /** * Union of all possible actions */ export type InterruptAction = SubmitAction | CancelAction | SuccessAction | FailureAction | RetryAction | ResetAction; /** * Result of a state transition */ export interface TransitionResult { /** The new state after transition */ state: InterruptState; /** Whether the transition was valid */ valid: boolean; /** Error message if transition was invalid */ error?: string; } /** * Initial idle state */ export declare const initialState: IdleState; /** * Transition function for the interrupt state machine * * @param state - Current state * @param action - Action to apply * @returns New state after transition * * @example * ```typescript * let state = initialState; * * // User submits * const result1 = transition(state, { type: "SUBMIT", value: true }); * if (result1.valid) state = result1.state; * * // API succeeds * const result2 = transition(state, { type: "SUCCESS" }); * if (result2.valid) state = result2.state; * ``` */ export declare function transition(state: InterruptState, action: InterruptAction): TransitionResult; /** * Check if the interrupt is in a terminal state (resolved or cancelled) * * @param state - The interrupt state * @returns True if the interrupt is finished */ export declare function isTerminalState(state: InterruptState): boolean; /** * Check if the interrupt is currently submitting * * @param state - The interrupt state * @returns True if submitting */ export declare function isSubmitting(state: InterruptState): boolean; /** * Check if the interrupt has an error * * @param state - The interrupt state * @returns True if in error state */ export declare function hasError(state: InterruptState): boolean; /** * Check if the interrupt can accept user input * * @param state - The interrupt state * @returns True if idle or error (can submit) */ export declare function canSubmit(state: InterruptState): boolean; /** * Get the error message if in error state * * @param state - The interrupt state * @returns Error message or undefined */ export declare function getErrorMessage(state: InterruptState): string | undefined; /** * Get the resolved value if in resolved state * * @param state - The interrupt state * @returns Resolved value or undefined */ export declare function getResolvedValue(state: InterruptState): unknown; /** * Map InterruptState status to legacy InterruptStatus for backward compatibility * * @param state - The interrupt state * @returns Legacy status string */ export declare function toLegacyStatus(state: InterruptState): 'pending' | 'resolved' | 'cancelled' | 'expired';