| 1 | interface DebounceOptions {
|
|---|
| 2 | /**
|
|---|
| 3 | * An optional AbortSignal to cancel the debounced function.
|
|---|
| 4 | */
|
|---|
| 5 | signal?: AbortSignal;
|
|---|
| 6 | /**
|
|---|
| 7 | * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|---|
| 8 | * If `edges` includes "leading", the function will be invoked at the start of the delay period.
|
|---|
| 9 | * If `edges` includes "trailing", the function will be invoked at the end of the delay period.
|
|---|
| 10 | * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
|
|---|
| 11 | * @default ["trailing"]
|
|---|
| 12 | */
|
|---|
| 13 | edges?: Array<'leading' | 'trailing'>;
|
|---|
| 14 | }
|
|---|
| 15 | interface DebouncedFunction<F extends (...args: any[]) => void> {
|
|---|
| 16 | (...args: Parameters<F>): void;
|
|---|
| 17 | /**
|
|---|
| 18 | * Schedules the execution of the debounced function after the specified debounce delay.
|
|---|
| 19 | * This method resets any existing timer, ensuring that the function is only invoked
|
|---|
| 20 | * after the delay has elapsed since the last call to the debounced function.
|
|---|
| 21 | * It is typically called internally whenever the debounced function is invoked.
|
|---|
| 22 | *
|
|---|
| 23 | * @returns {void}
|
|---|
| 24 | */
|
|---|
| 25 | schedule: () => void;
|
|---|
| 26 | /**
|
|---|
| 27 | * Cancels any pending execution of the debounced function.
|
|---|
| 28 | * This method clears the active timer and resets any stored context or arguments.
|
|---|
| 29 | */
|
|---|
| 30 | cancel: () => void;
|
|---|
| 31 | /**
|
|---|
| 32 | * Immediately invokes the debounced function if there is a pending execution.
|
|---|
| 33 | * This method executes the function right away if there is a pending execution.
|
|---|
| 34 | */
|
|---|
| 35 | flush: () => void;
|
|---|
| 36 | }
|
|---|
| 37 | /**
|
|---|
| 38 | * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|---|
| 39 | * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|---|
| 40 | * method to cancel any pending execution.
|
|---|
| 41 | *
|
|---|
| 42 | * @template F - The type of function.
|
|---|
| 43 | * @param {F} func - The function to debounce.
|
|---|
| 44 | * @param {number} debounceMs - The number of milliseconds to delay.
|
|---|
| 45 | * @param {DebounceOptions} options - The options object
|
|---|
| 46 | * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|---|
| 47 | * @returns A new debounced function with a `cancel` method.
|
|---|
| 48 | *
|
|---|
| 49 | * @example
|
|---|
| 50 | * const debouncedFunction = debounce(() => {
|
|---|
| 51 | * console.log('Function executed');
|
|---|
| 52 | * }, 1000);
|
|---|
| 53 | *
|
|---|
| 54 | * // Will log 'Function executed' after 1 second if not called again in that time
|
|---|
| 55 | * debouncedFunction();
|
|---|
| 56 | *
|
|---|
| 57 | * // Will not log anything as the previous call is canceled
|
|---|
| 58 | * debouncedFunction.cancel();
|
|---|
| 59 | *
|
|---|
| 60 | * // With AbortSignal
|
|---|
| 61 | * const controller = new AbortController();
|
|---|
| 62 | * const signal = controller.signal;
|
|---|
| 63 | * const debouncedWithSignal = debounce(() => {
|
|---|
| 64 | * console.log('Function executed');
|
|---|
| 65 | * }, 1000, { signal });
|
|---|
| 66 | *
|
|---|
| 67 | * debouncedWithSignal();
|
|---|
| 68 | *
|
|---|
| 69 | * // Will cancel the debounced function call
|
|---|
| 70 | * controller.abort();
|
|---|
| 71 | */
|
|---|
| 72 | declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal, edges }?: DebounceOptions): DebouncedFunction<F>;
|
|---|
| 73 |
|
|---|
| 74 | export { type DebounceOptions, type DebouncedFunction, debounce };
|
|---|