UNPKG

@webda/core

Version:

Expose API with Lambda

103 lines (102 loc) 2.61 kB
/** * Represent an item within the Throttler */ declare class ThrottlerItem { method: () => Promise<any>; callbacks: ((res?: any) => void)[]; name: string; /** * Once the promise is in progress */ promise?: Promise<any>; constructor(method: () => Promise<any>, callbacks: ((res?: any) => void)[], name: string); toString(): string; } /** * Promise Throttler * * Allow you to queue promise and execute them concurrently * * Several libraries does that * * The queue method still gives you a simple Promise linked to the resolution * of your queued item, so you can still wait for the execution of the specific * item */ export declare class Throttler { protected concurrency: number; /** * Current queue */ protected _queue: ThrottlerItem[]; /** * Number of promises in-progress */ current: number; /** * Resolver for each call to waitForCompletion */ protected _waiters: (() => void)[]; /** * * @param concurrency max concurrent promise to execute */ constructor(concurrency?: number); /** * Run a Throttler without having to instanciate it * @param method * @param concurrency * @returns */ static run(method: () => Promise<any> | (() => Promise<any>)[], concurrency?: number): Promise<void>; /** * Execute a new promise * * Alias for queue * @param method * @param name * @returns */ execute(method: () => Promise<any> | (() => Promise<any>)[], name?: string): Promise<any>; /** * Queue a new promise * * @param method executor that return the promise to queue * @param name of the task, usefull when calling getInProgress * @returns */ queue(method: () => Promise<any> | (() => Promise<any>)[], name?: string): Promise<any>; /** * Set the concurrency * @param concurrency newValue * * If decreased, it will be in effect only when current promises resolve * If increased, it will have immediate effect */ setConcurrency(concurrency: number): void; /** * Get inprogress items * @returns */ getInProgress(): ThrottlerItem[]; /** * Get global queue size * @returns */ getSize(): number; /** * @deprecated */ waitForCompletion(): Promise<void>; /** * Wait until every promise resolve * @returns */ wait(): Promise<void>; /** * Internal manage the promise concurrency * @returns */ protected add(): void; } export {};