| [a762898] | 1 | import { DebouncedFuncLeading, DebouncedFunc } from './debounce.mjs';
|
|---|
| 2 |
|
|---|
| 3 | interface ThrottleSettings {
|
|---|
| 4 | /**
|
|---|
| 5 | * If `true`, the function will be invoked on the leading edge of the timeout.
|
|---|
| 6 | * @default true
|
|---|
| 7 | */
|
|---|
| 8 | leading?: boolean | undefined;
|
|---|
| 9 | /**
|
|---|
| 10 | * If `true`, the function will be invoked on the trailing edge of the timeout.
|
|---|
| 11 | * @default true
|
|---|
| 12 | */
|
|---|
| 13 | trailing?: boolean | undefined;
|
|---|
| 14 | }
|
|---|
| 15 | type ThrottleSettingsLeading = (ThrottleSettings & {
|
|---|
| 16 | leading: true;
|
|---|
| 17 | }) | Omit<ThrottleSettings, 'leading'>;
|
|---|
| 18 | /**
|
|---|
| 19 | * Creates a throttled function that only invokes the provided function at most once
|
|---|
| 20 | * per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|---|
| 21 | * within the wait time will not trigger the execution of the original function.
|
|---|
| 22 | *
|
|---|
| 23 | * @template F - The type of function.
|
|---|
| 24 | * @param {F} func - The function to throttle.
|
|---|
| 25 | * @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
|---|
| 26 | * @param {ThrottleOptions} options - The options object
|
|---|
| 27 | * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the throttled function.
|
|---|
| 28 | * @param {boolean} options.leading - If `true`, the function will be invoked on the leading edge of the timeout.
|
|---|
| 29 | * @param {boolean} options.trailing - If `true`, the function will be invoked on the trailing edge of the timeout.
|
|---|
| 30 | * @returns {(...args: Parameters<F>) => void} A new throttled function that accepts the same parameters as the original function.
|
|---|
| 31 | *
|
|---|
| 32 | * @example
|
|---|
| 33 | * const throttledFunction = throttle(() => {
|
|---|
| 34 | * console.log('Function executed');
|
|---|
| 35 | * }, 1000);
|
|---|
| 36 | *
|
|---|
| 37 | * // Will log 'Function executed' immediately
|
|---|
| 38 | * throttledFunction();
|
|---|
| 39 | *
|
|---|
| 40 | * // Will not log anything as it is within the throttle time
|
|---|
| 41 | * throttledFunction();
|
|---|
| 42 | *
|
|---|
| 43 | * // After 1 second
|
|---|
| 44 | * setTimeout(() => {
|
|---|
| 45 | * throttledFunction(); // Will log 'Function executed'
|
|---|
| 46 | * }, 1000);
|
|---|
| 47 | */
|
|---|
| 48 | declare function throttle<T extends (...args: any) => any>(func: T, throttleMs?: number, options?: ThrottleSettingsLeading): DebouncedFuncLeading<T>;
|
|---|
| 49 | /**
|
|---|
| 50 | * Creates a throttled function that only invokes the provided function at most once
|
|---|
| 51 | * per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|---|
| 52 | * within the wait time will not trigger the execution of the original function.
|
|---|
| 53 | *
|
|---|
| 54 | * @template F - The type of function.
|
|---|
| 55 | * @param {F} func - The function to throttle.
|
|---|
| 56 | * @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
|---|
| 57 | * @param {ThrottleOptions} options - The options object
|
|---|
| 58 | * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the throttled function.
|
|---|
| 59 | * @param {boolean} options.leading - If `true`, the function will be invoked on the leading edge of the timeout.
|
|---|
| 60 | * @param {boolean} options.trailing - If `true`, the function will be invoked on the trailing edge of the timeout.
|
|---|
| 61 | * @returns {(...args: Parameters<F>) => void} A new throttled function that accepts the same parameters as the original function.
|
|---|
| 62 | *
|
|---|
| 63 | * @example
|
|---|
| 64 | * const throttledFunction = throttle(() => {
|
|---|
| 65 | * console.log('Function executed');
|
|---|
| 66 | * }, 1000);
|
|---|
| 67 | *
|
|---|
| 68 | * // Will log 'Function executed' immediately
|
|---|
| 69 | * throttledFunction();
|
|---|
| 70 | *
|
|---|
| 71 | * // Will not log anything as it is within the throttle time
|
|---|
| 72 | * throttledFunction();
|
|---|
| 73 | *
|
|---|
| 74 | * // After 1 second
|
|---|
| 75 | * setTimeout(() => {
|
|---|
| 76 | * throttledFunction(); // Will log 'Function executed'
|
|---|
| 77 | * }, 1000);
|
|---|
| 78 | */
|
|---|
| 79 | declare function throttle<T extends (...args: any) => any>(func: T, throttleMs?: number, options?: ThrottleSettings): DebouncedFunc<T>;
|
|---|
| 80 |
|
|---|
| 81 | export { throttle };
|
|---|