Skip to content

Commit

Permalink
vscode: add raceCancellationError-util, tweak raceCancellation-util a…
Browse files Browse the repository at this point in the history
…s well, microsoft/vscode#140557

Commit: 7855392ed42218367435398c2c4e5d1c983ad771
  • Loading branch information
Johannes Rieken authored and sourcegraph-bot committed Jan 12, 2022
1 parent 4a8394d commit 8e59c01
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions vscode/src/vs/base/common/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { canceled } from 'vs/base/common/errors';
import { CancellationError } from 'vs/base/common/errors';
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable, IDisposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { extUri as defaultExtUri, IExtUri } from 'vs/base/common/resources';
Expand All @@ -27,7 +27,7 @@ export function createCancelablePromise<T>(callback: (token: CancellationToken)
const subscription = source.token.onCancellationRequested(() => {
subscription.dispose();
source.dispose();
reject(canceled());
reject(new CancellationError());
});
Promise.resolve(thenable).then(value => {
subscription.dispose();
Expand Down Expand Up @@ -56,10 +56,40 @@ export function createCancelablePromise<T>(callback: (token: CancellationToken)
};
}

/**
* Returns a promise that resolves with `undefined` as soon as the passed token is cancelled.
* @see {@link raceCancellationError}
*/
export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken): Promise<T | undefined>;

/**
* Returns a promise that resolves with `defaultValue` as soon as the passed token is cancelled.
* @see {@link raceCancellationError}
*/
export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken, defaultValue: T): Promise<T>;

export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken, defaultValue?: T): Promise<T | undefined> {
return Promise.race([promise, new Promise<T | undefined>(resolve => token.onCancellationRequested(() => resolve(defaultValue)))]);
return new Promise((resolve, reject) => {
const ref = token.onCancellationRequested(() => {
ref.dispose();
resolve(defaultValue);
});
promise.then(resolve, reject).finally(() => ref.dispose());
});
}

/**
* Returns a promise that rejects with an {@CancellationError} as soon as the passed token is cancelled.
* @see {@link raceCancellation}
*/
export function raceCancellationError<T>(promise: Promise<T>, token: CancellationToken): Promise<T> {
return new Promise((resolve, reject) => {
const ref = token.onCancellationRequested(() => {
ref.dispose();
reject(new CancellationError());
});
promise.then(resolve, reject).finally(() => ref.dispose());
});
}

/**
Expand Down Expand Up @@ -325,7 +355,7 @@ export class Delayer<T> implements IDisposable {

if (this.completionPromise) {
if (this.doReject) {
this.doReject(canceled());
this.doReject(new CancellationError());
}
this.completionPromise = null;
}
Expand Down Expand Up @@ -441,7 +471,7 @@ export function timeout(millis: number, token?: CancellationToken): CancelablePr
const disposable = token.onCancellationRequested(() => {
clearTimeout(handle);
disposable.dispose();
reject(canceled());
reject(new CancellationError());
});
});
}
Expand Down Expand Up @@ -1085,7 +1115,7 @@ export interface IdleDeadline {
*/
export let runWhenIdle: (callback: (idle: IdleDeadline) => void, timeout?: number) => IDisposable;

declare function requestIdleCallback(callback: (args: IdleDeadline) => void, options?: { timeout: number }): number;
declare function requestIdleCallback(callback: (args: IdleDeadline) => void, options?: { timeout: number; }): number;
declare function cancelIdleCallback(handle: number): void;

(function () {
Expand Down Expand Up @@ -1386,7 +1416,7 @@ export class DeferredPromise<T> {

public cancel() {
new Promise<void>(resolve => {
this.errorCallback(canceled());
this.errorCallback(new CancellationError());
this.rejected = true;
resolve();
});
Expand Down Expand Up @@ -1489,7 +1519,7 @@ export interface AyncIterableExecutor<T> {
/**
* @param emitter An object that allows to emit async values valid only for the duration of the executor.
*/
(emitter: AsyncIterableEmitter<T>): void | Promise<void>
(emitter: AsyncIterableEmitter<T>): void | Promise<void>;
}

/**
Expand Down Expand Up @@ -1704,7 +1734,7 @@ export function createCancelableAsyncIterable<T>(callback: (token: CancellationT
const subscription = source.token.onCancellationRequested(() => {
subscription.dispose();
source.dispose();
emitter.reject(canceled());
emitter.reject(new CancellationError());
});
try {
for await (const item of innerIterable) {
Expand Down

0 comments on commit 8e59c01

Please sign in to comment.