| 1 | interface DebounceSettings {
|
|---|
| 2 | /**
|
|---|
| 3 | * If `true`, the function will be invoked on the leading edge of the timeout.
|
|---|
| 4 | * @default false
|
|---|
| 5 | */
|
|---|
| 6 | leading?: boolean | undefined;
|
|---|
| 7 | /**
|
|---|
| 8 | * The maximum time `func` is allowed to be delayed before it's invoked.
|
|---|
| 9 | * @default Infinity
|
|---|
| 10 | */
|
|---|
| 11 | maxWait?: number | undefined;
|
|---|
| 12 | /**
|
|---|
| 13 | * If `true`, the function will be invoked on the trailing edge of the timeout.
|
|---|
| 14 | * @default true
|
|---|
| 15 | */
|
|---|
| 16 | trailing?: boolean | undefined;
|
|---|
| 17 | }
|
|---|
| 18 | interface DebounceSettingsLeading extends DebounceSettings {
|
|---|
| 19 | leading: true;
|
|---|
| 20 | }
|
|---|
| 21 | interface DebouncedFunc<T extends (...args: any[]) => any> {
|
|---|
| 22 | /**
|
|---|
| 23 | * Call the original function, but applying the debounce rules.
|
|---|
| 24 | *
|
|---|
| 25 | * If the debounced function can be run immediately, this calls it and returns its return
|
|---|
| 26 | * value.
|
|---|
| 27 | *
|
|---|
| 28 | * Otherwise, it returns the return value of the last invocation, or undefined if the debounced
|
|---|
| 29 | * function was not invoked yet.
|
|---|
| 30 | */
|
|---|
| 31 | (...args: Parameters<T>): ReturnType<T> | undefined;
|
|---|
| 32 | /**
|
|---|
| 33 | * Throw away any pending invocation of the debounced function.
|
|---|
| 34 | */
|
|---|
| 35 | cancel(): void;
|
|---|
| 36 | /**
|
|---|
| 37 | * If there is a pending invocation of the debounced function, invoke it immediately and return
|
|---|
| 38 | * its return value.
|
|---|
| 39 | *
|
|---|
| 40 | * Otherwise, return the value from the last invocation, or undefined if the debounced function
|
|---|
| 41 | * was never invoked.
|
|---|
| 42 | */
|
|---|
| 43 | flush(): ReturnType<T> | undefined;
|
|---|
| 44 | }
|
|---|
| 45 | interface DebouncedFuncLeading<T extends (...args: any[]) => any> extends DebouncedFunc<T> {
|
|---|
| 46 | (...args: Parameters<T>): ReturnType<T>;
|
|---|
| 47 | flush(): ReturnType<T>;
|
|---|
| 48 | }
|
|---|
| 49 | /**
|
|---|
| 50 | * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|---|
| 51 | * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|---|
| 52 | * method to cancel any pending execution.
|
|---|
| 53 | *
|
|---|
| 54 | * You can set the debounced function to run at the start (`leading`) or end (`trailing`) of the delay period.
|
|---|
| 55 | * If `leading` is true, the function runs immediately on the first call.
|
|---|
| 56 | * If `trailing` is true, the function runs after `debounceMs` milliseconds have passed since the last call.
|
|---|
| 57 | * If both `leading` and `trailing` are true, the function runs at both the start and end, but it must be called at least twice within `debounceMs` milliseconds for this to happen
|
|---|
| 58 | * (since one debounced function call cannot trigger the function twice).
|
|---|
| 59 | *
|
|---|
| 60 | * You can also set a `maxWait` time, which is the maximum time the function is allowed to be delayed before it is called.
|
|---|
| 61 | *
|
|---|
| 62 | * @template F - The type of function.
|
|---|
| 63 | * @param {F} func - The function to debounce.
|
|---|
| 64 | * @param {number} debounceMs - The number of milliseconds to delay.
|
|---|
| 65 | * @param {DebounceOptions} options - The options object
|
|---|
| 66 | * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|---|
| 67 | * @param {boolean} options.leading - If `true`, the function will be invoked on the leading edge of the timeout.
|
|---|
| 68 | * @param {boolean} options.trailing - If `true`, the function will be invoked on the trailing edge of the timeout.
|
|---|
| 69 | * @param {number} options.maxWait - The maximum time `func` is allowed to be delayed before it's invoked.
|
|---|
| 70 | * @returns A new debounced function with a `cancel` method.
|
|---|
| 71 | *
|
|---|
| 72 | * @example
|
|---|
| 73 | * const debouncedFunction = debounce(() => {
|
|---|
| 74 | * console.log('Function executed');
|
|---|
| 75 | * }, 1000);
|
|---|
| 76 | *
|
|---|
| 77 | * // Will log 'Function executed' after 1 second if not called again in that time
|
|---|
| 78 | * debouncedFunction();
|
|---|
| 79 | *
|
|---|
| 80 | * // Will not log anything as the previous call is canceled
|
|---|
| 81 | * debouncedFunction.cancel();
|
|---|
| 82 | *
|
|---|
| 83 | * // With AbortSignal
|
|---|
| 84 | * const controller = new AbortController();
|
|---|
| 85 | * const signal = controller.signal;
|
|---|
| 86 | * const debouncedWithSignal = debounce(() => {
|
|---|
| 87 | * console.log('Function executed');
|
|---|
| 88 | * }, 1000, { signal });
|
|---|
| 89 | *
|
|---|
| 90 | * debouncedWithSignal();
|
|---|
| 91 | *
|
|---|
| 92 | * // Will cancel the debounced function call
|
|---|
| 93 | * controller.abort();
|
|---|
| 94 | */
|
|---|
| 95 | declare function debounce<T extends (...args: any) => any>(func: T, wait: number | undefined, options: DebounceSettingsLeading): DebouncedFuncLeading<T>;
|
|---|
| 96 | /**
|
|---|
| 97 | * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|---|
| 98 | * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|---|
| 99 | * method to cancel any pending execution.
|
|---|
| 100 | *
|
|---|
| 101 | * You can set the debounced function to run at the start (`leading`) or end (`trailing`) of the delay period.
|
|---|
| 102 | * If `leading` is true, the function runs immediately on the first call.
|
|---|
| 103 | * If `trailing` is true, the function runs after `debounceMs` milliseconds have passed since the last call.
|
|---|
| 104 | * If both `leading` and `trailing` are true, the function runs at both the start and end, but it must be called at least twice within `debounceMs` milliseconds for this to happen
|
|---|
| 105 | * (since one debounced function call cannot trigger the function twice).
|
|---|
| 106 | *
|
|---|
| 107 | * You can also set a `maxWait` time, which is the maximum time the function is allowed to be delayed before it is called.
|
|---|
| 108 | *
|
|---|
| 109 | * @template F - The type of function.
|
|---|
| 110 | * @param {F} func - The function to debounce.
|
|---|
| 111 | * @param {number} debounceMs - The number of milliseconds to delay.
|
|---|
| 112 | * @param {DebounceOptions} options - The options object
|
|---|
| 113 | * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|---|
| 114 | * @param {boolean} options.leading - If `true`, the function will be invoked on the leading edge of the timeout.
|
|---|
| 115 | * @param {boolean} options.trailing - If `true`, the function will be invoked on the trailing edge of the timeout.
|
|---|
| 116 | * @param {number} options.maxWait - The maximum time `func` is allowed to be delayed before it's invoked.
|
|---|
| 117 | * @returns A new debounced function with a `cancel` method.
|
|---|
| 118 | *
|
|---|
| 119 | * @example
|
|---|
| 120 | * const debouncedFunction = debounce(() => {
|
|---|
| 121 | * console.log('Function executed');
|
|---|
| 122 | * }, 1000);
|
|---|
| 123 | *
|
|---|
| 124 | * // Will log 'Function executed' after 1 second if not called again in that time
|
|---|
| 125 | * debouncedFunction();
|
|---|
| 126 | *
|
|---|
| 127 | * // Will not log anything as the previous call is canceled
|
|---|
| 128 | * debouncedFunction.cancel();
|
|---|
| 129 | *
|
|---|
| 130 | * // With AbortSignal
|
|---|
| 131 | * const controller = new AbortController();
|
|---|
| 132 | * const signal = controller.signal;
|
|---|
| 133 | * const debouncedWithSignal = debounce(() => {
|
|---|
| 134 | * console.log('Function executed');
|
|---|
| 135 | * }, 1000, { signal });
|
|---|
| 136 | *
|
|---|
| 137 | * debouncedWithSignal();
|
|---|
| 138 | *
|
|---|
| 139 | * // Will cancel the debounced function call
|
|---|
| 140 | * controller.abort();
|
|---|
| 141 | */
|
|---|
| 142 | declare function debounce<T extends (...args: any) => any>(func: T, wait?: number, options?: DebounceSettings): DebouncedFunc<T>;
|
|---|
| 143 |
|
|---|
| 144 | export { type DebouncedFunc, type DebouncedFuncLeading, debounce };
|
|---|