UNPKG

function-performer

Version:

Performer providing API for debounce, throttle, deduplication and limiting functions

52 lines (51 loc) 2.47 kB
import { type DebouncedFunction, type DebouncedFunctionConfig } from './debounce'; import { type ThrottledFunction, type ThrottledFunctionConfig } from './throttle'; import { type DeduplicatedFunction, type DeduplicatedFunctionConfig } from './deduplicate'; import { type LimitedFunction, type LimitedFunctionConfig } from './limit'; type AnyFunction = (...args: any[]) => any; type ParamsExcludeCount<TFunc extends AnyFunction> = Parameters<TFunc> extends [number, ...infer _Params] ? _Params : []; interface IPerformerFunction { (func: AnyFunction, ...args: any[]): any; } interface IPerformerProperty<TFunc extends IPerformerFunction, TConfig extends object> { with: (config: TConfig) => TFunc; } interface IDebouncedPropertyFunction { <TFunc extends DebouncedFunction>(func: TFunc, ...args: Parameters<TFunc>): Promise<ReturnType<TFunc>>; } interface IThrottledPropertyFunction { <TFunc extends ThrottledFunction>(func: TFunc, ...args: Parameters<TFunc>): Promise<ReturnType<TFunc>>; } interface IDeduplicatedPropertyFunction { <TFunc extends DeduplicatedFunction>(func: TFunc, ...args: ParamsExcludeCount<TFunc>): Promise<ReturnType<TFunc>>; } interface ILimitedPropertyFunction { <TFunc extends LimitedFunction>(func: TFunc, ...args: Parameters<TFunc>): ReturnType<TFunc> | void; } interface IDebounceProperty extends IDebouncedPropertyFunction, IPerformerProperty<IDebouncedPropertyFunction, DebouncedFunctionConfig> { } interface IThrottleProperty extends IThrottledPropertyFunction, IPerformerProperty<IThrottledPropertyFunction, ThrottledFunctionConfig> { } interface IDeduplicateProperty extends IDeduplicatedPropertyFunction, IPerformerProperty<IDeduplicatedPropertyFunction, DeduplicatedFunctionConfig> { } interface ILimitProperty extends ILimitedPropertyFunction, IPerformerProperty<ILimitedPropertyFunction, LimitedFunctionConfig> { } type PerformerConfig = { debounce?: DebouncedFunctionConfig; throttle?: ThrottledFunctionConfig; deduplicate?: DeduplicatedFunctionConfig; limit?: LimitedFunctionConfig; }; declare class Performer { readonly debounce: IDebounceProperty; readonly throttle: IThrottleProperty; readonly deduplicate: IDeduplicateProperty; readonly limit: ILimitProperty; constructor(config?: PerformerConfig); private _define; private _defineProperty; private _deconfigure; private _configure; } export { Performer, type PerformerConfig }; export default Performer;