UNPKG

@barchart/common-js

Version:
69 lines (68 loc) 3.82 kB
/** * An object that wraps asynchronous delays (i.e. timeout and interval). * * @public * @extends {Disposable} */ export default class Scheduler extends Disposable { /** * @public * @static * @async * @param {Function} actionToSchedule * @param {number} millisecondDelay * @param {string=} actionDescription * @returns {Promise} */ public static schedule(actionToSchedule: Function, millisecondDelay: number, actionDescription?: string | undefined): Promise<any>; /** * @public * @static * @async * @param {Function} actionToBackoff * @param {number} millisecondDelay * @param {string=} actionDescription * @param {number=} maximumAttempts * @param {Function=} failureCallback * @param {object=} failureValue * @param {number=} maximumDelay * @returns {Promise} */ public static backoff(actionToBackoff: Function, millisecondDelay: number, actionDescription?: string | undefined, maximumAttempts?: number | undefined, failureCallback?: Function | undefined, failureValue?: object | undefined, maximumDelay?: number | undefined): Promise<any>; /** * Schedules an action to execute in the future, returning a Promise. * * @public * @async * @param {Function} actionToSchedule - The action to execute. * @param {number} millisecondDelay - Milliseconds before the action can be started. * @param {string=} actionDescription - A description of the action, used for logging purposes. * @returns {Promise} */ public schedule(actionToSchedule: Function, millisecondDelay: number, actionDescription?: string | undefined): Promise<any>; /** * @public * @param {Function} actionToRepeat * @param {number} millisecondInterval * @param {string=} actionDescription * @returns {Disposable} */ public repeat(actionToRepeat: Function, millisecondInterval: number, actionDescription?: string | undefined): Disposable; /** * Attempts an action, repeating if necessary, using an exponential backoff. * * @public * @async * @param {Function} actionToBackoff - The action to attempt. If it fails -- because an error is thrown, a promise is rejected, or the function returns a falsey value -- the action will be invoked again. * @param {number=} millisecondDelay - The amount of time to wait to execute the action. Subsequent failures are multiply this value by 2 ^ [number of failures]. So, a 1000 millisecond backoff would schedule attempts using the following delays: 0, 1000, 2000, 4000, 8000, etc. If not specified, the first attempt will execute immediately, then a value of 1000 will be used. * @param {string=} actionDescription - Description of the action to attempt, used for logging purposes. * @param {number=} maximumAttempts - The number of attempts to before giving up. * @param {Function=} failureCallback - If provided, will be invoked if a function is considered to be failing. * @param {object=} failureValue - If provided, will consider the result to have failed, if this value is returned (a deep equality check is used). If not provided, an undefined value will trigger a retry. * @param {number=} maximumDelay - The maximum delay that can be used for the backoff. If not provided, the delay will continue to double until the maximum number of attempts is reached. * @returns {Promise} */ public backoff(actionToBackoff: Function, millisecondDelay?: number | undefined, actionDescription?: string | undefined, maximumAttempts?: number | undefined, failureCallback?: Function | undefined, failureValue?: object | undefined, maximumDelay?: number | undefined): Promise<any>; #private; } import Disposable from './../lang/Disposable.js';