@wordpress/compose
Version:
WordPress higher-order components (HOCs).
38 lines • 2.02 kB
TypeScript
export interface ThrottleOptions {
leading?: boolean;
trailing?: boolean;
}
/**
* A simplified and properly typed version of lodash's `throttle`, that
* always uses timers instead of sometimes using rAF.
*
* Creates a throttled function that only invokes `func` at most once per
* every `wait` milliseconds. The throttled function comes with a `cancel`
* method to cancel delayed `func` invocations and a `flush` method to
* immediately invoke them. Provide `options` to indicate whether `func`
* should be invoked on the leading and/or trailing edge of the `wait`
* timeout. The `func` is invoked with the last arguments provided to the
* throttled function. Subsequent calls to the throttled function return
* the result of the last `func` invocation.
*
* **Note:** If `leading` and `trailing` options are `true`, `func` is
* invoked on the trailing edge of the timeout only if the throttled function
* is invoked more than once during the `wait` timeout.
*
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
* until the next tick, similar to `setTimeout` with a timeout of `0`.
*
* @param {Function} func The function to throttle.
* @param {number} wait The number of milliseconds to throttle invocations to.
* @param {Partial< ThrottleOptions >} options The options object.
* @param {boolean} options.leading Specify invoking on the leading edge of the timeout.
* @param {boolean} options.trailing Specify invoking on the trailing edge of the timeout.
* @return Returns the new throttled function.
*/
export declare const throttle: <FunctionT extends (...args: unknown[]) => unknown>(func: FunctionT, wait: number, options?: ThrottleOptions) => {
(this: unknown, ...args: Parameters<FunctionT>): ReturnType<FunctionT>;
cancel: () => void;
flush: () => ReturnType<FunctionT>;
pending: () => boolean;
};
//# sourceMappingURL=index.d.ts.map