/** * @file Time travel middleware combining history, snapshots, and replay capabilities */ import type { Context, BaseMachine } from '../index'; import { type HistoryEntry, type Serializer } from './history'; import { type ContextSnapshot } from './snapshot'; /** * A machine enhanced with history tracking capabilities. * * @typeParam M - Original machine type. */ export type WithHistory> = M & { /** History of all transitions */ history: HistoryEntry[]; /** Clear all history */ clearHistory: () => void; }; /** * A machine enhanced with snapshot tracking capabilities. * * @typeParam M - Original machine type. */ export type WithSnapshot> = M & { /** Snapshots of context before/after each transition */ snapshots: ContextSnapshot>[]; /** Clear all snapshots */ clearSnapshots: () => void; /** Restore machine to a previous context state */ restoreSnapshot: (context: Context) => M; }; /** * A machine enhanced with time travel capabilities. */ type TimeTravelResult = R extends Promise ? V extends BaseMachine ? Promise> : R : R extends BaseMachine ? WithTimeTravel : R; type TimeTravelMachine> = { [K in keyof M]: M[K] extends (...args: infer A) => infer R ? (...args: A) => TimeTravelResult : M[K]; }; /** * Machine enhanced with history, context snapshots, restoration, and replay. * * Transition parameter lists are preserved. Machine-returning transitions are * recursively wrapped so debugging capabilities remain available afterward. * * @typeParam M - Machine type being instrumented. */ export type WithTimeTravel> = TimeTravelMachine & { /** History of all transitions */ history: HistoryEntry[]; /** Snapshots of context before/after each transition */ snapshots: ContextSnapshot>[]; /** Clear all history and snapshots */ clearTimeTravel: () => void; /** Clear just the history */ clearHistory: () => void; /** Clear just the snapshots */ clearSnapshots: () => void; /** Restore machine to a previous context state */ restoreSnapshot: (context: Context) => M; /** Replay transitions from a specific point in history */ replayFrom: (startIndex: number) => M; }; /** * Creates a machine with full time travel debugging capabilities. * Combines history tracking, snapshots, and replay functionality. * * @template M - The machine type * @param machine - The machine to enhance * @param options - Configuration options * @returns A machine with time travel capabilities * * @example * ```typescript * const debugMachine = withTimeTravel(counter); * * // Make some transitions * debugMachine.increment(); * debugMachine.increment(); * debugMachine.decrement(); * * // Time travel to previous states * const previousState = debugMachine.replayFrom(0); // Replay from start * const undoLast = debugMachine.restoreSnapshot(debugMachine.snapshots[1].before); * * // Inspect history * console.log(debugMachine.history); * console.log(debugMachine.snapshots); * ``` */ export declare function withTimeTravel>(machine: M, options?: { /** Maximum number of history entries/snapshots to keep */ maxSize?: number; /** Optional serializer for persistence */ serializer?: Serializer; /** Callback when history/snapshot events occur */ onRecord?: (type: 'history' | 'snapshot', data: any) => void; }): WithTimeTravel; export {}; //# sourceMappingURL=time-travel.d.ts.map