src/vs/base/common/cancellation.ts
206 LOC · 200 covered · 6 uncovered · 66 ranges · 17277 concepts · 31 introducers · 10744 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
cancellation.ts ×18
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from './event.js';
import { DisposableStore, IDisposable } from './lifecycle.js';
export interface CancellationToken {
/**
* A flag signalling is cancellation has been requested.
*/
readonly isCancellationRequested: boolean;
/**
* An event which fires when cancellation is requested. This event
* only ever fires `once` as cancellation can only happen once. Listeners
* that are registered after cancellation will be called (next event loop run),
* but also only once.
*
* @event
*/
readonly onCancellationRequested: (listener: (e: void) => unknown, thisArgs?: unknown, disposables?: IDisposable[]) => IDisposable;
}
const shortcutEvent: Event<void> = Object.freeze(function (callback, context?): IDisposable {
return { dispose() { clearTimeout(handle); } };
});
export namespace CancellationToken {
export function isCancellationToken(thing: unknown): thing is CancellationToken {
if (thing === CancellationToken.None || thing === CancellationToken.Cancelled) {
cancellation.ts ×3
}
}
}
return typeof (thing as CancellationToken).isCancellationRequested === 'boolean'
&& typeof (thing as CancellationToken).onCancellationRequested === 'function';
export const None = Object.freeze<CancellationToken>({
isCancellationRequested: false,
onCancellationRequested: Event.None
});
export const Cancelled = Object.freeze<CancellationToken>({
isCancellationRequested: true,
onCancellationRequested: shortcutEvent
});
}
private _isCancelled: boolean = false;
private _emitter: Emitter<void> | null = null;
public cancel() {
this._isCancelled = true;
if (this._emitter) {
this.dispose();
}
}
get isCancellationRequested(): boolean {
}
get onCancellationRequested(): Event<void> {
}
this._emitter = new Emitter<void>();
}
return this._emitter.event;
public dispose(): void {
this._emitter = null;
}
export class CancellationTokenSource {
private _token?: CancellationToken = undefined;
private _parentListener?: IDisposable = undefined;
constructor(parent?: CancellationToken) {
this._parentListener = parent && parent.onCancellationRequested(this.cancel, this);
cancellation.ts ×1
}
get token(): CancellationToken {
// actually needed
this._token = new MutableToken();
}
}
cancel(): void {
// cancelled token when cancellation happens
// before someone asks for the token
this._token = CancellationToken.Cancelled;
this._token.cancel();
}
dispose(cancel: boolean = false): void {
}
if (!this._token) {
this._token = CancellationToken.None;
this._token.dispose();
}
export function cancelOnDispose(store: DisposableStore): CancellationToken {
const source = new CancellationTokenSource();
store.add({ dispose() { source.cancel(); } });
return source.token;
}
/**
* A pool that aggregates multiple cancellation tokens. The pool's own token
* (accessible via `pool.token`) is cancelled only after every token added
* to the pool has been cancelled. Adding tokens after the pool token has
* been cancelled has no effect.
*/
export class CancellationTokenPool {
private readonly _source = new CancellationTokenSource();
private readonly _listeners = new DisposableStore();
private _total: number = 0;
private _cancelled: number = 0;
private _isDone: boolean = false;
get token(): CancellationToken {
}
/**
* Add a token to the pool. If the token is already cancelled it is counted
* immediately. Tokens added after the pool token has been cancelled are ignored.
*/
add(token: CancellationToken): void {
}
this._total++;
if (token.isCancellationRequested) {
this._check();
return;
}
const d = token.onCancellationRequested(() => {
this._cancelled++;
this._check();
this._listeners.add(d);
private _check(): void {
this._isDone = true;
this._listeners.dispose();
this._source.cancel();
}
}
dispose(): void {
this._source.dispose();
}