UNPKG

thorish

Version:

This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.

53 lines (52 loc) 1.41 kB
/** * Configuration for {@link buildLimiter}. */ export type LimitConfig = { /** * Maximum and initial total of tokens in this limiter. * * This must be `>=1` to be valid. */ b: number; /** * Rate of renewal of tokens, per second. * * This must be `>0` to be valid. */ r: number; }; /** * Fetches a {@link Promise} which resolves when a token is available. * * Throws if the {@link AbortSignal} is or becomes aborted before a token is available. */ export type Limiter = (signal: AbortSignal) => Promise<void>; /** * Builds a rate-limiter which can be called to consume a token. * * Throws if the config is invalid (cowardly refuses to make an invalid limiter). * Without a passed config, uses a default of 100 tokens, renews at 10/sec. */ export declare function buildLimiter(c?: LimitConfig): Limiter; export interface Backoff { readonly delay: number; /** * Delays by this long. * Abandons (but does not throw) if the signal aborts. */ timeout(signal?: AbortSignal): Promise<void>; /** * Indicate that we've been successful. * Resets the delay. */ success(): void; /** * Indicate that an error has occured here. * Increases the delay. */ error(): void; } /** * Creates a simple backoff helper. */ export declare function createBackoff(baseDelay?: number): Backoff;