@kvaser/canking-api
Version:
CanKing API to communicate with the CanKing service using Node.js.
111 lines (110 loc) • 4.23 kB
TypeScript
/**
* The undo/redo state.
* @typeParam T - The type of the undo/redo data.
*/
export interface IUndoState<T> {
/** The previous values */
previous: T[];
/** The current value */
current?: T;
/** The next values */
next: T[];
}
/**
* The undo/redo object. Includes the current state and methods to manipulate it.
* @typeParam T - The type of the undo/redo data.
*/
export interface IUndoObject<T> {
/** The current undo/redo state */
state: IUndoState<T>;
/**
* Set a new current state
* @param updater - The new value or a function that receives the current value and returns the new value
*/
set: (updater: T | ((curr?: T) => T)) => void;
/**
* Reset the undo/redo history with a new initial value
* @param current - The new initial value. If undefined, current will be unset.
* @param previous - The previous values (optional)
* @param next - The next values (optional)
*/
reset: (current?: T, previous?: T[], next?: T[]) => void;
/**
* Undo to the previous state
*/
undo: () => void;
/**
* Redo to the next state
*/
redo: () => void;
/**
* Check if undo is possible
* @returns true if there are states to undo to
*/
canUndo: () => boolean;
/**
* Check if redo is possible
* @returns true if there are states to redo to
*/
canRedo: () => boolean;
/**
* Subscribe to state changes (optional, for triggering re-renders)
* @param listener - Callback function that will be called whenever state changes
* @returns Unsubscribe function
*/
subscribe?: (listener: () => void) => () => void;
}
/**
* Helper function to limit buffer size by keeping only the most recent entries.
* @param buffer - The buffer to limit.
* @param maxLength - Maximum allowed length.
* @returns Limited buffer.
* @internal
*/
export declare function limitBuffer<T>(buffer: T[], maxLength: number): T[];
/**
* Performs an undo operation on the undo/redo state.
* @param state - Current undo/redo state.
* @param maxBufferLength - Maximum buffer length for history.
* @returns New undo/redo state after undo.
* @internal
*/
export declare function performUndo<T>(state: IUndoState<T>, maxBufferLength: number): IUndoState<T>;
/**
* Performs a redo operation on the undo/redo state.
* @param state - Current undo/redo state.
* @param maxBufferLength - Maximum buffer length for history.
* @returns New undo/redo state after redo.
* @internal
*/
export declare function performRedo<T>(state: IUndoState<T>, maxBufferLength: number): IUndoState<T>;
/**
* Performs a set operation on the undo/redo state.
* @param state - Current undo/redo state.
* @param newValue - New value to set.
* @param maxBufferLength - Maximum buffer length for history.
* @param equalityFn - Optional equality function to check if value changed.
* @returns New undo/redo state after set, or null if value didn't change.
* @internal
*/
export declare function performSet<T>(state: IUndoState<T>, newValue: T, maxBufferLength: number, equalityFn?: (a: T | undefined, b: T) => boolean): IUndoState<T> | null;
/**
* Custom hook to manage undo/redo state using a ref (does not trigger re-renders by default).
* @remarks
* This hook uses useRef and does not automatically trigger component re-renders.
* Use the subscribe method if you need to trigger re-renders on state changes, or use useUndo instead.
* @typeParam T - The type of the undo/redo data.
* @param initialState - The initial undo state.
* @returns An undo object for managing state history.
*/
export declare function useUndoRef<T>(initialState?: IUndoState<T>): IUndoObject<T>;
/**
* Custom hook to manage undo/redo state with automatic re-renders.
* @remarks
* This hook triggers component re-renders whenever the state changes.
* If you don't need re-renders, use useUndoRef instead for better performance.
* @typeParam T - The type of the state being managed.
* @param initialState - The initial undo state.
* @returns An undo object for managing state history with automatic re-renders.
*/
export declare function useUndo<T>(initialState?: IUndoState<T>): IUndoObject<T>;