UNPKG

4.62 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17/*---------------------------------------------------------------------------------------------
18 * Copyright (c) Microsoft Corporation and others. All rights reserved.
19 * Licensed under the MIT License. See https://github.com/Microsoft/vscode/blob/master/LICENSE.txt for license information.
20 *--------------------------------------------------------------------------------------------*/
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.checkCancelled = exports.isCancelled = exports.cancelled = exports.CancellationTokenSource = exports.CancellationError = exports.CancellationToken = void 0;
23const event_1 = require("./event");
24const types_1 = require("./types");
25// eslint-disable-next-line @typescript-eslint/no-explicit-any
26const shortcutEvent = Object.freeze(Object.assign(function (callback, context) {
27 const handle = setTimeout(callback.bind(context), 0);
28 return { dispose() { clearTimeout(handle); } };
29}, {
30 get maxListeners() { return 0; },
31 set maxListeners(maxListeners) { }
32}));
33var CancellationToken;
34(function (CancellationToken) {
35 CancellationToken.None = Object.freeze({
36 isCancellationRequested: false,
37 onCancellationRequested: event_1.Event.None
38 });
39 CancellationToken.Cancelled = Object.freeze({
40 isCancellationRequested: true,
41 onCancellationRequested: shortcutEvent
42 });
43 function is(value) {
44 return (0, types_1.isObject)(value) && (value === CancellationToken.None
45 || value === CancellationToken.Cancelled
46 || ((0, types_1.isBoolean)(value.isCancellationRequested) && !!value.onCancellationRequested));
47 }
48 CancellationToken.is = is;
49})(CancellationToken = exports.CancellationToken || (exports.CancellationToken = {}));
50class CancellationError extends Error {
51 constructor() {
52 super('Canceled');
53 this.name = this.message;
54 }
55}
56exports.CancellationError = CancellationError;
57class MutableToken {
58 constructor() {
59 this._isCancelled = false;
60 }
61 cancel() {
62 if (!this._isCancelled) {
63 this._isCancelled = true;
64 if (this._emitter) {
65 this._emitter.fire(undefined);
66 this._emitter = undefined;
67 }
68 }
69 }
70 get isCancellationRequested() {
71 return this._isCancelled;
72 }
73 get onCancellationRequested() {
74 if (this._isCancelled) {
75 return shortcutEvent;
76 }
77 if (!this._emitter) {
78 this._emitter = new event_1.Emitter();
79 }
80 return this._emitter.event;
81 }
82}
83class CancellationTokenSource {
84 get token() {
85 if (!this._token) {
86 // be lazy and create the token only when
87 // actually needed
88 this._token = new MutableToken();
89 }
90 return this._token;
91 }
92 cancel() {
93 if (!this._token) {
94 // save an object by returning the default
95 // cancelled token when cancellation happens
96 // before someone asks for the token
97 this._token = CancellationToken.Cancelled;
98 }
99 else if (this._token !== CancellationToken.Cancelled) {
100 this._token.cancel();
101 }
102 }
103 dispose() {
104 this.cancel();
105 }
106}
107exports.CancellationTokenSource = CancellationTokenSource;
108const cancelledMessage = 'Cancelled';
109function cancelled() {
110 return new Error(cancelledMessage);
111}
112exports.cancelled = cancelled;
113function isCancelled(err) {
114 return !!err && err.message === cancelledMessage;
115}
116exports.isCancelled = isCancelled;
117function checkCancelled(token) {
118 if (!!token && token.isCancellationRequested) {
119 throw cancelled();
120 }
121}
122exports.checkCancelled = checkCancelled;
123//# sourceMappingURL=cancellation.js.map
\No newline at end of file