import { CancellationToken } from './cancellation'; import { IDisposable } from './disposable'; export type MaybePromise = T | Promise | PromiseLike; export declare const FRAME_ONE = 16; export declare const FRAME_TWO: number; export declare const FRAME_THREE: number; export declare const FRAME_FOUR: number; export declare const FRAME_FIVE: number; export interface CancelablePromise extends Promise { cancel(): void; } export interface MayCancelablePromise extends Promise { cancel?(): void; } export declare function createCancelablePromise(callback: (token: CancellationToken) => Promise): CancelablePromise; export declare function hookCancellationToken(token: CancellationToken, promise: Promise): PromiseLike; export type ITask = () => T; /** * A helper to prevent accumulation of sequential async tasks. * * Imagine a mail man with the sole task of delivering letters. As soon as * a letter submitted for delivery, he drives to the destination, delivers it * and returns to his base. Imagine that during the trip, N more letters were submitted. * When the mail man returns, he picks those N letters and delivers them all in a * single trip. Even though N+1 submissions occurred, only 2 deliveries were made. * * The throttler implements this via the queue() method, by providing it a task * factory. Following the example: * * const throttler = new Throttler(); * const letters = []; * * function deliver() { * const lettersToDeliver = letters; * letters = []; * return makeTheTrip(lettersToDeliver); * } * * function onLetterReceived(l) { * letters.push(l); * throttler.queue(deliver); * } */ export declare class Throttler { private activePromise; private queuedPromise; private queuedPromiseFactory; constructor(); queue(promiseFactory: ITask>): Promise; } export declare class Sequencer { private current; queue(promiseTask: ITask>): Promise; } /** * A helper to delay execution of a task that is being requested often. * * Following the throttler, now imagine the mail man wants to optimize the number of * trips proactively. The trip itself can be long, so he decides not to make the trip * as soon as a letter is submitted. Instead he waits a while, in case more * letters are submitted. After said waiting period, if no letters were submitted, he * decides to make the trip. Imagine that N more letters were submitted after the first * one, all within a short period of time between each other. Even though N+1 * submissions occurred, only 1 delivery was made. * * The delayer offers this behavior via the trigger() method, into which both the task * to be executed and the waiting period (delay) must be passed in as arguments. Following * the example: * * const delayer = new Delayer(WAITING_PERIOD); * const letters = []; * * function letterReceived(l) { * letters.push(l); * delayer.trigger(() => { return makeTheTrip(); }); * } */ export declare class Delayer implements IDisposable { defaultDelay: number; private timeout; private completionPromise; private doResolve; private doReject?; private task; constructor(defaultDelay: number); trigger(task: ITask>, delay?: number): Promise; isTriggered(): boolean; cancel(): void; private cancelTimeout; dispose(): void; } /** * A helper to delay execution of a task that is being requested often, while * preventing accumulation of consecutive executions, while the task runs. * * The mail man is clever and waits for a certain amount of time, before going * out to deliver letters. While the mail man is going out, more letters arrive * and can only be delivered once he is back. Once he is back the mail man will * do one more trip to deliver the letters that have accumulated while he was out. */ export declare class ThrottledDelayer { private delayer; private throttler; constructor(defaultDelay: number); trigger(promiseFactory: ITask>, delay?: number): Promise; isTriggered(): boolean; cancel(): void; dispose(): void; } /** * A barrier that is initially closed and then becomes opened permanently. */ export declare class Barrier { private _isOpen; private _promise; private _completePromise; constructor(); isOpen(): boolean; open(): void; wait(): Promise; reject(): void; } /** * A barrier that is initially closed and then becomes opened permanently after a certain period of * time or when open is called explicitly */ export declare class AutoOpenBarrier extends Barrier { private readonly _timeout; constructor(autoOpenTimeMs: number); open(): void; } export declare function isThenable(obj: any): obj is Promise; export declare function raceTimeout(promise: Promise, timeout: number, onTimeout?: () => void): Promise; export declare function asPromise(callback: () => T | Thenable): Promise; export interface IdleDeadline { readonly didTimeout: boolean; timeRemaining(): DOMHighResTimeStamp; } /** * Execute the callback the next time the browser is idle */ export declare let runWhenIdle: (callback: (idle: IdleDeadline) => void, timeout?: number) => IDisposable; /** * An implementation of the "idle-until-urgent"-strategy as introduced * here: https://philipwalton.com/articles/idle-until-urgent/ */ export declare class IdleValue { private readonly _executor; private readonly _handle; private _didRun; private _value?; private _error; constructor(executor: () => T); dispose(): void; getValue(): T; } export type Mutable = { -readonly [P in keyof T]: T[P]; }; export declare function first(promiseFactories: ITask>[], shouldStop?: (t: T) => boolean, defaultValue?: T | null): Promise; export declare function timeout(millis: number): CancelablePromise; export declare function timeout(millis: number, token: CancellationToken): Promise; export declare function raceCancellation(promise: Promise, token: CancellationToken): Promise; export declare function raceCancellation(promise: Promise, token: CancellationToken, defaultValue: T): Promise; export declare class RunOnceScheduler { protected runner: ((...args: unknown[]) => void) | null; private timeoutToken; private timeout; private timeoutHandler; constructor(runner: (...args: any[]) => void, delay: number); /** * Dispose RunOnceScheduler */ dispose(): void; /** * Cancel current scheduled runner (if any). */ cancel(): void; /** * Cancel previous runner (if any) & schedule a new runner. */ schedule(delay?: number): void; trigger(): void; get delay(): number; set delay(value: number); /** * Returns true if scheduled. */ isScheduled(): boolean; private onTimeout; protected doRun(): void; } export declare function disposableTimeout(handler: () => void, timeout?: number): IDisposable; export declare function sleep(time: number): Promise; export declare const retry: (task: () => Promise, options: { delay: number; retries: number; onFailedAttempt?: (error: any) => void; timeout?: number; }) => Promise; export declare class StateTracer { protected deferred: { [state: string]: Barrier; }; has(state: string): boolean; delete(state: string): void; record(state: string): void; fulfill(state: string): void; reachedState(state: string): Promise; reachedAnyState(...states: string[]): Promise; dispose(): void; } //# sourceMappingURL=async.d.ts.map