UNPKG

debounce-toolkit

Version:

A debounce utility with multiple execution modes

60 lines (57 loc) 1.91 kB
/** * Debounce execution modes */ declare enum DebounceMode { /** Execute function after the delay (most common) */ TRAILING = "trailing", /** Execute function immediately, then wait */ LEADING = "leading", /** Execute immediately AND after delay */ BOTH = "both" } /** * A debounced function with additional utility methods */ interface DebouncedFunction<T extends (...args: any[]) => any> { /** * The debounced version of the original function */ (...args: Parameters<T>): void; /** * Cancels any pending execution */ cancel(): void; /** * Immediately executes any pending call */ flush(): void; /** * Returns true if there's a pending execution */ pending(): boolean; } /** * Options for configuring debounce behavior */ interface DebounceOptions { /** The debounce mode: TRAILING, LEADING, or BOTH */ mode?: DebounceMode; } /** * Creates a debounced version of a function that delays invoking func until * after wait milliseconds have elapsed since the last time the debounced * function was invoked. * * @param fn - The function to debounce * @param wait - The number of milliseconds to delay (default: 0) * @param mode - The debounce mode: TRAILING, LEADING, or BOTH (default: TRAILING) * @returns The debounced function with additional methods: * - cancel(): Cancels any pending execution * - flush(): Immediately executes any pending call * - pending(): Returns true if there's a pending execution * */ declare function debounce<T extends (...args: any[]) => any>(fn: T, wait?: number, mode?: DebounceMode): DebouncedFunction<T>; declare function debounce<T extends (...args: any[]) => any>(fn: T, wait?: number, options?: DebounceOptions): DebouncedFunction<T>; export { DebounceMode, debounce, debounce as default }; export type { DebounceOptions, DebouncedFunction };