callagain
Version:
Call again failed calls
64 lines (63 loc) • 2.12 kB
TypeScript
export interface CallAgainOptions {
maxConcurrentCalls?: number;
maxRetryAttempts?: number;
delayOnRetry?: number;
maxCallsPerInterval?: number;
intervalLength?: number;
}
export declare type NextHandler = (err: any) => boolean;
export declare type ErrorHandler = (err: any, next: NextHandler) => boolean;
declare type Args<F> = F extends (...args: infer T) => any ? T : never;
declare type Return<F> = F extends (...args: any[]) => infer T ? T : never;
declare type Promised<T> = T extends PromiseLike<any> ? T : Promise<T>;
export declare class CallAgain {
private readonly _maxConcurrentCalls?;
private readonly _maxRetryAttempts;
private readonly _delayOnRetry;
private readonly _maxCallsPerInterval?;
private readonly _intervalLength?;
private _cycleTimer?;
private readonly _db;
private readonly _entries;
private readonly _history;
private _errorHandlers;
private _lastId;
private _rejected;
constructor(options?: CallAgainOptions);
/**
* Creates new function from provided. New function is controlled by CallAgain
* @param {Function} f - function that needs to be controlled
* @return {Function} controlled function
*/
wrap<F extends Function>(f: F): (...args: Args<F>) => Promised<Return<F>>;
/**
* Registers error handler
* @param handler
*/
onError(handler: ErrorHandler): this;
/**
* Rejects all current and pending calls
* @param reason object
*/
rejectAll(reason?: any): void;
/**
* Set timeout to call cycle method next time
* @private
*/
private _planNextCycle;
private _calculateNextCycleDelay;
private _calculateCallsLeftBeforeConcurrentLimit;
private _calculateCallsLeftBeforeIntervalLimit;
private _onCycleTimerTick;
private _processEntry;
private _shouldRetryOnError;
/**
* Is called by wrapped function
* @param {function} func - source function
* @param args - arguments passed to wrapped function
* @private
*/
private _onWrapperCalled;
private _getNewId;
}
export {};