/** * @file Snapshot tracking middleware for context state */ import type { Context, BaseMachine } from '../index'; import type { Serializer } from './history'; /** * A recorded context pair surrounding one transition. * * @typeParam C - Machine context captured before and after the transition. */ export interface ContextSnapshot { /** Unique ID for this snapshot */ id: string; /** Name of the transition that caused this snapshot */ transitionName: string; /** Context before the transition */ before: C; /** Context after the transition */ after: C; /** Timestamp of the snapshot */ timestamp: number; /** Optional serialized versions of contexts */ serializedBefore?: string; serializedAfter?: string; /** Optional diff information */ diff?: any; } type SnapshotResult = R extends Promise ? V extends BaseMachine ? Promise> : R : R extends BaseMachine ? SnapshotTrackedMachine : R; type SnapshotMachine> = { [K in keyof M]: M[K] extends (...args: infer A) => infer R ? (...args: A) => SnapshotResult : M[K]; }; /** * A machine whose machine-returning transitions preserve snapshot tracking. * * `restoreSnapshot(context)` creates a machine at the supplied context; it does * not mutate an earlier immutable machine snapshot. * * @typeParam M - Original machine type. */ export type SnapshotTrackedMachine> = SnapshotMachine & { snapshots: ContextSnapshot>[]; clearSnapshots: () => void; restoreSnapshot: (context: Context) => M; }; /** * Creates a machine with snapshot tracking capabilities. * Records context state before and after each transition for debugging and inspection. * * @template M - The machine type * @param machine - The machine to track * @param options - Configuration options * @returns A new machine with snapshot tracking * * @example * ```typescript * const tracked = withSnapshot(counter, { * maxSize: 50, * serializer: { * serialize: (ctx) => JSON.stringify(ctx), * deserialize: (str) => JSON.parse(str) * } * }); * * tracked.increment(); * console.log(tracked.snapshots); // [{ before: { count: 0 }, after: { count: 1 }, ... }] * ``` */ export declare function withSnapshot>(machine: M, options?: { /** Maximum number of snapshots to keep (default: unlimited) */ maxSize?: number; /** Optional serializer for context */ serializer?: Serializer>; /** Custom function to capture additional snapshot data */ captureSnapshot?: (before: Context, after: Context) => any; /** Only capture snapshots where context actually changed */ onlyOnChange?: boolean; }): SnapshotTrackedMachine; export {}; //# sourceMappingURL=snapshot.d.ts.map