froebel
Version:
TypeScript utility library
28 lines (27 loc) • 1.21 kB
TypeScript
import type { λ } from "./types";
/**
* Returns a version of the function `fun` that can only be invoked `limit`
* times.
* An optional `except` function will be called with the same parameters on any
* additional invocations.
*
* If `fun` returns anything but `void` (or `Promise<void>`), supplying an
* `except` function is mandatory.
*
* The `except` function must have the same return type as `fun`, or — if `fun`
* returns a promise — it may return the type that the promise resolves to
* synchronously.
*
* The `except` function may also throw instead of returning a value.
*/
export declare const limitInvocations: <T extends λ<any[], any>>(fun: T, limit: number, ...[except]: ExcS<T>) => T;
/**
* Special case of {@link limitInvocations}. `fun` can only be invoked once.
*
* @see {@link limitInvocations}
*/
export declare const once: <T extends λ<any[], any>>(fun: T, ...[except]: ExcS<T>) => T;
declare type ExcS<T extends λ> = ReturnType<T> extends void | PromiseLike<void> ? [except?: Exc<T>] : [except: Exc<T>];
declare type Exc<T extends λ> = λ<Parameters<T>, OptProm<ReturnType<T>>>;
declare type OptProm<T> = T extends Promise<infer I> ? I | Promise<I> : T;
export {};