import { type ConfigureOptions } from './config.ts'; import type { ChangeListener, ChangeRecord, ConflictResolutions, DiffRecord, ListenerOptions, MergeResult, PathMode, PathSelector } from './types.ts'; export interface ApiDeps { getRoot: (obj: object) => object; } export interface ChronicleApiMethods { /** * Subscribe to changes at a specific path in the object. The listener is called whenever * the selected property or any of its descendants change. Returns an unsubscribe function. * * @param object - The chronicled object to listen to * @param selector - Function that selects the path to watch (e.g., `obj => obj.user.name`) * @param listener - Callback invoked on changes with (path, newValue, oldValue, meta) * @returns Unsubscribe function to stop listening * * @example * ```ts * const unsubscribe = chronicle.listen(state, s => s.count, (path, newVal, oldVal) => { * console.log(`count changed from ${oldVal} to ${newVal}`); * }); * ``` */ listen(object: T, selector: PathSelector, listener: ChangeListener): () => void; /** * Subscribe to changes at a specific path with listener options like debouncing or throttling. * * @param object - The chronicled object to listen to * @param selector - Function that selects the path to watch * @param listener - Callback invoked on changes * @param options - Listener configuration (once, debounceMs, throttleMs, schedule) * @returns Unsubscribe function */ listen(object: T, selector: PathSelector, listener: ChangeListener, options: ListenerOptions): () => void; /** * Subscribe to changes at a specific path with a traversal mode. * * @param object - The chronicled object to listen to * @param selector - Function that selects the path to watch * @param listener - Callback invoked on changes * @param mode - Path matching mode: 'exact' (only this path), 'up' (ancestors), 'down' (descendants) * @returns Unsubscribe function */ listen(object: T, selector: PathSelector, listener: ChangeListener, mode: PathMode): () => void; /** * Subscribe to changes at a specific path with both traversal mode and listener options. * * @param object - The chronicled object to listen to * @param selector - Function that selects the path to watch * @param listener - Callback invoked on changes * @param mode - Path matching mode: 'exact', 'up', or 'down' * @param options - Listener configuration (once, debounceMs, throttleMs, schedule) * @returns Unsubscribe function */ listen(object: T, selector: PathSelector, listener: ChangeListener, mode: PathMode, options: ListenerOptions): () => void; /** * Subscribe to all changes on the object, regardless of path. Useful for broad monitoring. * * @param obj - The chronicled object to listen to * @param listener - Callback invoked on any change with (path, newValue, oldValue, meta) * @param options - Optional listener configuration (once, debounceMs, throttleMs, schedule) * @returns Unsubscribe function to stop listening */ onAny: (obj: object, listener: ChangeListener, options?: ListenerOptions) => () => void; /** * Temporarily pause change notifications for this object. Changes still occur and are * recorded in history, but listeners won't be notified until resume() is called. * * @param obj - The chronicled object to pause */ pause: (obj: object) => void; /** * Resume change notifications that were paused. Accumulated changes are not batched; * call flush() if you want to trigger pending notifications immediately. * * @param obj - The chronicled object to resume */ resume: (obj: object) => void; /** * Immediately flush any pending change notifications that are scheduled asynchronously. * Useful when you need synchronous notification delivery. * * @param obj - The chronicled object to flush */ flush: (obj: object) => void; /** * Get a copy of the complete change history for this object. Each record contains * path, type, oldValue, newValue, timestamp, and groupId for undo/redo operations. * * @param obj - The chronicled object * @returns Array of change records (copy, safe to modify) */ getHistory: (obj: object) => ChangeRecord[]; /** * Clear all change history and redo cache. The object's current state is preserved, * but undo/redo will no longer work until new changes are made. * * @param obj - The chronicled object */ clearHistory: (obj: object) => void; /** * Reset the object to its pristine state (the state when first chronicled or when * markPristine was last called). This is an un-doable operation. * * @param obj - The chronicled object to reset */ reset: (obj: object) => void; /** * Mark the current state as the new pristine baseline. Clears history and resets the * snapshot used for diff() and reset(). Useful after saving to server or committing changes. * * @param obj - The chronicled object */ markPristine: (obj: object) => void; /** * Get the differences between the current state and the pristine state. Returns an array * of changes showing what was added, removed, or modified. * * @param obj - The chronicled object * @returns Array of diff records with path, kind ('added'|'removed'|'changed'), and values */ diff: (obj: object) => DiffRecord[]; /** * Check if the object has any changes from its pristine state. Returns true if the object * matches its original or last markPristine() state. * * @param obj - The chronicled object * @returns True if no changes exist, false otherwise */ isPristine: (obj: object) => boolean; /** * Create a deep clone snapshot of the current state. The snapshot is a plain object, * not a chronicle proxy, and can be serialized or stored independently. * * @param obj - The chronicled object * @returns Deep clone of the current state */ snapshot: (obj: T) => T; /** * Get the original unwrapped object (removes the chronicle proxy). Use with caution: * changes to the unwrapped object won't be tracked or trigger listeners. * * @param obj - The chronicled object * @returns The underlying object without proxy wrapper */ unwrap: (obj: T) => T; /** * Perform a three-way merge between the pristine state (base), current state (ours), * and incoming changes (theirs). Detects conflicts and allows resolution strategies. * Useful for syncing local changes with server updates. * * @param obj - The chronicled object * @param incomingObject - The incoming state to merge * @param resolutions - Optional conflict resolution strategies per path * @returns Merge result with success status, conflicts array, and count of applied changes */ merge: (obj: object, incomingObject: object, resolutions?: ConflictResolutions) => MergeResult; /** * Get a marker representing the current history length. Use with undoSince() to undo * changes made after this point. Useful for transaction-like rollback behavior. * * @param obj - The chronicled object * @returns Current history length (marker value) */ mark: (obj: object) => number; /** * Undo the last N changes (or all changes if steps not specified). Changes are undone * individually. For grouped operations, use undoGroups() instead. * * @param obj - The chronicled object * @param steps - Number of changes to undo (default: all) */ undo: (obj: object, steps?: number) => void; /** * Undo all changes made since the specified marker. The marker should come from mark(). * This undoes everything after that point in history. * * @param obj - The chronicled object * @param historyLengthBefore - The marker value from mark() */ undoSince: (obj: object, historyLengthBefore: number) => void; /** * Undo the last N undo groups (batches/transactions). Unlike undo() which works on * individual changes, this respects the grouping from batches and transactions. * * @param obj - The chronicled object * @param groups - Number of groups to undo (default: 1) */ undoGroups: (obj: object, groups?: number) => void; /** * Check if undo is available (history has changes that can be undone). * * @param obj - The chronicled object * @returns True if undo is possible, false otherwise */ canUndo: (obj: object) => boolean; /** * Check if redo is available (there are undone changes that can be reapplied). * * @param obj - The chronicled object * @returns True if redo is possible, false otherwise */ canRedo: (obj: object) => boolean; /** * Clear the redo cache, making undone changes un-re-doable. The object's current state * and undo history are preserved. * * @param obj - The chronicled object */ clearRedo: (obj: object) => void; /** * Redo the last N undone changes (or all undone changes if steps not specified). * Changes are redone individually. * * @param obj - The chronicled object * @param steps - Number of changes to redo (default: all) */ redo: (obj: object, steps?: number) => void; /** * Redo the last N undo groups. Unlike redo() which works on individual changes, * this respects the grouping from batches and transactions. * * @param obj - The chronicled object * @param groups - Number of groups to redo (default: 1) */ redoGroups: (obj: object, groups?: number) => void; /** * Configure chronicle options for this object. Options include history limits, merge * behavior, custom clone/compare functions, and proxy caching. Can be called multiple * times; new options are merged with existing ones. * * @param obj - The chronicled object * @param options - Configuration options to apply */ configure: (obj: object, options: ConfigureOptions) => void; } export declare const createApiMethods: (deps: ApiDeps) => ChronicleApiMethods; //# sourceMappingURL=api-methods.d.ts.map