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.

1 > /*--------------------------------------------------------------------------------------------- cancellation.ts ×18
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { Emitter, Event } from './event.js';
7 > import { DisposableStore, IDisposable } from './lifecycle.js';
8 >
9 > export interface CancellationToken {
10 >
11 > /**
12 > * A flag signalling is cancellation has been requested.
13 > */
14 > readonly isCancellationRequested: boolean;
15 >
16 > /**
17 > * An event which fires when cancellation is requested. This event
18 > * only ever fires `once` as cancellation can only happen once. Listeners
19 > * that are registered after cancellation will be called (next event loop run),
20 > * but also only once.
21 > *
22 > * @event
23 > */
24 > readonly onCancellationRequested: (listener: (e: void) => unknown, thisArgs?: unknown, disposables?: IDisposable[]) => IDisposable;
25 > }
26 >
27 > const shortcutEvent: Event<void> = Object.freeze(function (callback, context?): IDisposable {
28 > const handle = setTimeout(callback.bind(context), 0); cancellation.ts ×1
29 > return { dispose() { clearTimeout(handle); } };
30 > });
32 > export namespace CancellationToken {
33 >
34 > export function isCancellationToken(thing: unknown): thing is CancellationToken {
35 > if (thing === CancellationToken.None || thing === CancellationToken.Cancelled) { cancellation.ts ×3
36 > return true; cancellation.ts ×1
37 > }
38 > if (thing instanceof MutableToken) { cancellation.ts ×1
39 > return true; cancellation.ts ×1
40 > }
41 > if (!thing || typeof thing !== 'object') { cancellation.ts ×3
42 > return false; cancellation.ts ×1
43 > }
44 return typeof (thing as CancellationToken).isCancellationRequested === 'boolean'
45 && typeof (thing as CancellationToken).onCancellationRequested === 'function';
48 >
49 > export const None = Object.freeze<CancellationToken>({
50 > isCancellationRequested: false,
51 > onCancellationRequested: Event.None
52 > });
53 >
54 > export const Cancelled = Object.freeze<CancellationToken>({
55 > isCancellationRequested: true,
56 > onCancellationRequested: shortcutEvent
57 > });
58 > }
59 >
60 > class MutableToken implements CancellationToken { cancellation.ts ×2
61 >
62 > private _isCancelled: boolean = false;
63 > private _emitter: Emitter<void> | null = null;
65 > public cancel() {
66 > if (!this._isCancelled) { cancellation.ts ×3
67 > this._isCancelled = true;
68 > if (this._emitter) {
69 > this._emitter.fire(undefined); cancellation.ts ×1
70 > this.dispose();
71 > }
73 > }
75 > get isCancellationRequested(): boolean {
76 > return this._isCancelled; cancellation.ts ×1
77 > }
79 > get onCancellationRequested(): Event<void> {
80 > if (this._isCancelled) { cancellation.ts ×2
81 > return shortcutEvent; cancellation.ts ×1
82 > }
83 > if (!this._emitter) { cancellation.ts ×1
84 > this._emitter = new Emitter<void>();
85 > }
86 > return this._emitter.event;
89 > public dispose(): void {
90 > if (this._emitter) { cancellation.ts ×2
91 > this._emitter.dispose(); cancellation.ts ×1
92 > this._emitter = null;
93 > }
96 >
97 > export class CancellationTokenSource {
98 >
99 > private _token?: CancellationToken = undefined;
100 > private _parentListener?: IDisposable = undefined;
101 >
102 > constructor(parent?: CancellationToken) {
103 > this._parentListener = parent && parent.onCancellationRequested(this.cancel, this); cancellation.ts ×1
104 > }
106 > get token(): CancellationToken {
107 > if (!this._token) { cancellation.ts ×2
108 > // be lazy and create the token only when cancellation.ts ×2
109 > // actually needed
110 > this._token = new MutableToken();
111 > }
112 > return this._token; cancellation.ts ×2
113 > }
115 > cancel(): void {
116 > if (!this._token) { cancellation.ts ×3
117 > // save an object by returning the default cancellation.ts ×1
118 > // cancelled token when cancellation happens
119 > // before someone asks for the token
120 > this._token = CancellationToken.Cancelled;
121 >
122 > } else if (this._token instanceof MutableToken) { cancellation.ts ×3
123 > // actually cancel cancellation.ts ×3
124 > this._token.cancel();
125 > }
128 > dispose(cancel: boolean = false): void {
129 > if (cancel) { cancellation.ts ×4
130 > this.cancel(); cancellation.ts ×1
131 > }
132 > this._parentListener?.dispose(); cancellation.ts ×4
133 > if (!this._token) {
134 > // ensure to initialize with an empty token if we had none cancellation.ts ×1
135 > this._token = CancellationToken.None;
136 >
137 > } else if (this._token instanceof MutableToken) { cancellation.ts ×4
138 > // actually dispose cancellation.ts ×1
139 > this._token.dispose();
140 > }
143 >
144 > export function cancelOnDispose(store: DisposableStore): CancellationToken {
145 const source = new CancellationTokenSource();
146 store.add({ dispose() { source.cancel(); } });
147 return source.token;
148 }
150 > /**
151 > * A pool that aggregates multiple cancellation tokens. The pool's own token
152 > * (accessible via `pool.token`) is cancelled only after every token added
153 > * to the pool has been cancelled. Adding tokens after the pool token has
154 > * been cancelled has no effect.
155 > */
156 > export class CancellationTokenPool {
158 > private readonly _source = new CancellationTokenSource();
159 > private readonly _listeners = new DisposableStore();
160 >
161 > private _total: number = 0;
162 > private _cancelled: number = 0;
163 > private _isDone: boolean = false;
165 > get token(): CancellationToken {
166 > return this._source.token; cancellation.ts ×3
167 > }
169 > /**
170 > * Add a token to the pool. If the token is already cancelled it is counted
171 > * immediately. Tokens added after the pool token has been cancelled are ignored.
172 > */
173 > add(token: CancellationToken): void {
174 > if (this._isDone) { cancellation.ts ×3
175 > return; cancellation.ts ×1
176 > }
178 > this._total++;
179 >
180 > if (token.isCancellationRequested) {
181 > this._cancelled++; cancellation.ts ×1
182 > this._check();
183 > return;
184 > }
186 > const d = token.onCancellationRequested(() => {
187 > d.dispose(); cancellation.ts ×1
188 > this._cancelled++;
189 > this._check();
191 > this._listeners.add(d);
194 > private _check(): void {
195 > if (!this._isDone && this._total > 0 && this._total === this._cancelled) { cancellation.ts ×1
196 > this._isDone = true;
197 > this._listeners.dispose();
198 > this._source.cancel();
199 > }
200 > }
202 > dispose(): void {
203 > this._listeners.dispose(); cancellation.ts ×3
204 > this._source.dispose();
205 > }