@storm-stack/utilities
Version:
This package includes various base utility class and various functions to assist in the development process.
42 lines (41 loc) • 1.37 kB
TypeScript
export interface DebounceOptions {
signal?: AbortSignal;
}
/**
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
* method to cancel any pending execution.
*
* @example
* ```typescript
* const debouncedFunction = debounce(() => {
* console.log('Function executed');
* }, 1000);
*
* // Will log 'Function executed' after 1 second if not called again in that time
* debouncedFunction();
*
* // Will not log anything as the previous call is canceled
* debouncedFunction.cancel();
*
* // With AbortSignal
* const controller = new AbortController();
* const signal = controller.signal;
* const debouncedWithSignal = debounce(() => {
* console.log('Function executed');
* }, 1000, { signal });
*
* debouncedWithSignal();
*
* // Will cancel the debounced function call
* controller.abort();
* ```
*
* @param func - The function to debounce.
* @param debounceMs - The number of milliseconds to delay.
* @param options - The options object.
* @returns A new debounced function with a `cancel` method.
*/
export declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal }?: DebounceOptions): F & {
cancel: () => void;
};