/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { CancellationToken, CancellationTokenSource } from './cancellation'; import { canceled } from '../../../vs/base/common/errors'; import { IDisposable, toDisposable } from '../../../vs/base/common/lifecycle'; export function isThenable(obj: unknown): obj is Promise { return !!obj && typeof (obj as unknown as Promise).then === 'function'; } export interface CancelablePromise extends Promise { cancel(): void; } export function createCancelablePromise( callback: (token: CancellationToken) => Promise ): CancelablePromise { const source = new CancellationTokenSource(); const thenable = callback(source.token); const promise = new Promise((resolve, reject) => { source.token.onCancellationRequested(() => { reject(canceled()); }); Promise.resolve(thenable).then( (value) => { source.dispose(); resolve(value); }, (err) => { source.dispose(); reject(err); } ); }); return >new (class { cancel() { source.cancel(); } then< TResult1 = T, TResult2 = never >(resolve?: ((value: T) => TResult1 | Promise) | undefined | null, reject?: ((reason: any) => TResult2 | Promise) | undefined | null): Promise { return promise.then(resolve, reject); } catch< TResult = never >(reject?: ((reason: any) => TResult | Promise) | undefined | null): Promise { return this.then(undefined, reject); } finally(onfinally?: (() => void) | undefined | null): Promise { return promise.finally(onfinally); } })(); } export function raceCancellation( promise: Promise, token: CancellationToken ): Promise; export function raceCancellation( promise: Promise, token: CancellationToken, defaultValue: T ): Promise; export function raceCancellation( promise: Promise, token: CancellationToken, defaultValue?: T ): Promise { return Promise.race([ promise, new Promise((resolve) => token.onCancellationRequested(() => resolve(defaultValue)) ), ]); } export interface 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 class Throttler { private activePromise: Promise | null; private queuedPromise: Promise | null; private queuedPromiseFactory: ITask> | null; constructor() { this.activePromise = null; this.queuedPromise = null; this.queuedPromiseFactory = null; } queue(promiseFactory: ITask>): Promise { if (this.activePromise) { this.queuedPromiseFactory = promiseFactory; if (!this.queuedPromise) { const onComplete = () => { this.queuedPromise = null; const result = this.queue(this.queuedPromiseFactory!); this.queuedPromiseFactory = null; return result; }; this.queuedPromise = new Promise((resolve) => { this.activePromise!.then(onComplete, onComplete).then(resolve); }); } return new Promise((resolve, reject) => { this.queuedPromise!.then(resolve, reject); }); } this.activePromise = promiseFactory(); return new Promise((resolve, reject) => { this.activePromise!.then( (result: T) => { this.activePromise = null; resolve(result); }, (err: unknown) => { this.activePromise = null; reject(err); } ); }); } } /** * A helper to delay (debounce) 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 class Delayer implements IDisposable { private timeout: any; private completionPromise: Promise | null; private doResolve: ((value?: any | Promise) => void) | null; private doReject: ((err: any) => void) | null; private task: ITask> | null; constructor(public defaultDelay: number) { this.timeout = null; this.completionPromise = null; this.doResolve = null; this.doReject = null; this.task = null; } trigger( task: ITask>, delay: number = this.defaultDelay ): Promise { this.task = task; this.cancelTimeout(); if (!this.completionPromise) { this.completionPromise = new Promise((resolve, reject) => { this.doResolve = resolve; this.doReject = reject; }).then(() => { this.completionPromise = null; this.doResolve = null; if (this.task) { const task = this.task; this.task = null; return task(); } return undefined; }); } this.timeout = setTimeout(() => { this.timeout = null; if (this.doResolve) { this.doResolve(null); } }, delay); return this.completionPromise; } isTriggered(): boolean { return this.timeout !== null; } cancel(): void { this.cancelTimeout(); if (this.completionPromise) { if (this.doReject) { this.doReject(canceled()); } this.completionPromise = null; } } private cancelTimeout(): void { if (this.timeout !== null) { clearTimeout(this.timeout); this.timeout = null; } } dispose(): void { this.cancelTimeout(); } } /** * 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 class ThrottledDelayer { private delayer: Delayer>; private throttler: Throttler; constructor(defaultDelay: number) { this.delayer = new Delayer(defaultDelay); this.throttler = new Throttler(); } trigger(promiseFactory: ITask>, delay?: number): Promise { return this.delayer.trigger( () => this.throttler.queue(promiseFactory), delay ) as unknown as Promise; } cancel(): void { this.delayer.cancel(); } dispose(): void { this.delayer.dispose(); } } export function timeout(millis: number): CancelablePromise; export function timeout( millis: number, token: CancellationToken ): Promise; export function timeout( millis: number, token?: CancellationToken ): CancelablePromise | Promise { if (!token) { return createCancelablePromise((token) => timeout(millis, token)); } return new Promise((resolve, reject) => { const handle = setTimeout(resolve, millis); token.onCancellationRequested(() => { clearTimeout(handle); reject(canceled()); }); }); } export function disposableTimeout( handler: () => void, timeout = 0 ): IDisposable { const timer = setTimeout(handler, timeout); return toDisposable(() => clearTimeout(timer)); } export function first( promiseFactories: ITask>[], shouldStop: (t: T) => boolean = (t) => !!t, defaultValue: T | null = null ): Promise { let index = 0; const len = promiseFactories.length; const loop: () => Promise = () => { if (index >= len) { return Promise.resolve(defaultValue); } const factory = promiseFactories[index++]; const promise = Promise.resolve(factory()); return promise.then((result) => { if (shouldStop(result)) { return Promise.resolve(result); } return loop(); }); }; return loop(); } export class TimeoutTimer implements IDisposable { private _token: any; constructor(); constructor(runner: () => void, timeout: number); constructor(runner?: () => void, timeout?: number) { this._token = -1; if (typeof runner === 'function' && typeof timeout === 'number') { this.setIfNotSet(runner, timeout); } } dispose(): void { this.cancel(); } cancel(): void { if (this._token !== -1) { clearTimeout(this._token); this._token = -1; } } cancelAndSet(runner: () => void, timeout: number): void { this.cancel(); this._token = setTimeout(() => { this._token = -1; runner(); }, timeout); } setIfNotSet(runner: () => void, timeout: number): void { if (this._token !== -1) { // timer is already set return; } this._token = setTimeout(() => { this._token = -1; runner(); }, timeout); } } export class IntervalTimer implements IDisposable { private _token: any; constructor() { this._token = -1; } dispose(): void { this.cancel(); } cancel(): void { if (this._token !== -1) { clearInterval(this._token); this._token = -1; } } cancelAndSet(runner: () => void, interval: number): void { this.cancel(); this._token = setInterval(() => { runner(); }, interval); } } export class RunOnceScheduler { protected runner: ((...args: unknown[]) => void) | null; private timeoutToken: any; private timeout: number; private timeoutHandler: () => void; constructor(runner: (...args: any[]) => void, delay: number) { this.timeoutToken = -1; this.runner = runner; this.timeout = delay; this.timeoutHandler = this.onTimeout.bind(this); } /** * Dispose RunOnceScheduler */ dispose(): void { this.cancel(); this.runner = null; } /** * Cancel current scheduled runner (if any). */ cancel(): void { if (this.isScheduled()) { clearTimeout(this.timeoutToken); this.timeoutToken = -1; } } /** * Cancel previous runner (if any) & schedule a new runner. */ schedule(delay = this.timeout): void { this.cancel(); this.timeoutToken = setTimeout(this.timeoutHandler, delay); } get delay(): number { return this.timeout; } set delay(value: number) { this.timeout = value; } /** * Returns true if scheduled. */ isScheduled(): boolean { return this.timeoutToken !== -1; } private onTimeout() { this.timeoutToken = -1; if (this.runner) { this.doRun(); } } protected doRun(): void { if (this.runner) { this.runner(); } } } //#region -- run on idle tricks ------------ export interface IdleDeadline {} /** * Execute the callback the next time the browser is idle */ export let runWhenIdle: ( callback: (idle: IdleDeadline) => void, timeout?: number ) => IDisposable; declare function requestIdleCallback( callback: (args: IdleDeadline) => void, options?: { timeout: number } ): number; declare function cancelIdleCallback(handle: number): void; (function () { if ( typeof requestIdleCallback !== 'function' || typeof cancelIdleCallback !== 'function' ) { const dummyIdle: IdleDeadline = Object.freeze({ didTimeout: true, timeRemaining() { return 15; }, }); runWhenIdle = (runner) => { const handle = setTimeout(() => runner(dummyIdle)); let disposed = false; return { dispose() { if (disposed) { return; } disposed = true; clearTimeout(handle); }, }; }; } else { runWhenIdle = (runner, timeout?) => { const handle: number = requestIdleCallback( runner, typeof timeout === 'number' ? { timeout } : undefined ); let disposed = false; return { dispose() { if (disposed) { return; } disposed = true; cancelIdleCallback(handle); }, }; }; } })(); /** * An implementation of the "idle-until-urgent"-strategy as introduced * here: https://philipwalton.com/articles/idle-until-urgent/ */ export class IdleValue { private readonly _executor: () => void; private readonly _handle: IDisposable; private _didRun: boolean = false; private _value?: T; private _error: unknown; constructor(executor: () => T) { this._executor = () => { try { this._value = executor(); } catch (err) { this._error = err; } finally { this._didRun = true; } }; this._handle = runWhenIdle(() => this._executor()); } dispose(): void { this._handle.dispose(); } get value(): T { if (!this._didRun) { this._handle.dispose(); this._executor(); } if (this._error) { throw this._error; } return this._value!; } } //#endregion //#region Promises export namespace Promises { export interface IResolvedPromise { status: 'fulfilled'; value: T; } export interface IRejectedPromise { status: 'rejected'; reason: Error; } /** * Interface of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled */ interface PromiseWithAllSettled { allSettled( promises: Promise[] ): Promise | IRejectedPromise)[]>; } /** * A polyfill of `Promise.allSettled`: returns after all promises have * resolved or rejected and provides access to each result or error * in the order of the original passed in promises array. * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled */ export async function allSettled( promises: Promise[] ): Promise | IRejectedPromise)[]> { if ( typeof (Promise as unknown as PromiseWithAllSettled).allSettled === 'function' ) { return allSettledNative(promises); // in some environments we can benefit from native implementation } return allSettledShim(promises); } async function allSettledNative( promises: Promise[] ): Promise | IRejectedPromise)[]> { return (Promise as unknown as PromiseWithAllSettled).allSettled( promises ); } async function allSettledShim( promises: Promise[] ): Promise | IRejectedPromise)[]> { return Promise.all( promises.map((promise) => promise.then( (value) => { const fulfilled: IResolvedPromise = { status: 'fulfilled', value, }; return fulfilled; }, (error) => { const rejected: IRejectedPromise = { status: 'rejected', reason: error, }; return rejected; } ) ) ); } /** * A drop-in replacement for `Promise.all` with the only difference * that the method awaits every promise to either fulfill or reject. * * Similar to `Promise.all`, only the first error will be returned * if any. */ export async function settled(promises: Promise[]): Promise { let firstError: Error | undefined = undefined; const result = await Promise.all( promises.map((promise) => promise.then( (value) => value, (error) => { if (!firstError) { firstError = error; } return undefined; // do not rethrow so that other promises can settle } ) ) ); if (typeof firstError !== 'undefined') { throw firstError; } return result as unknown as T[]; // cast is needed and protected by the `throw` above } } //#endregion