2 /*---------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License. See License.txt in the project root for license information.
5 *--------------------------------------------------------------------------------------------*/
6 Object.defineProperty(exports, "__esModule", { value: true });
7 exports.CancellationTokenSource = exports.CancellationToken = void 0;
8 const ral_1 = require("./ral");
9 const Is = require("./is");
10 const events_1 = require("./events");
11 var CancellationToken;
12 (function (CancellationToken) {
13 CancellationToken.None = Object.freeze({
14 isCancellationRequested: false,
15 onCancellationRequested: events_1.Event.None
17 CancellationToken.Cancelled = Object.freeze({
18 isCancellationRequested: true,
19 onCancellationRequested: events_1.Event.None
22 const candidate = value;
23 return candidate && (candidate === CancellationToken.None
24 || candidate === CancellationToken.Cancelled
25 || (Is.boolean(candidate.isCancellationRequested) && !!candidate.onCancellationRequested));
27 CancellationToken.is = is;
28 })(CancellationToken = exports.CancellationToken || (exports.CancellationToken = {}));
29 const shortcutEvent = Object.freeze(function (callback, context) {
30 const handle = ral_1.default().timer.setTimeout(callback.bind(context), 0);
31 return { dispose() { ral_1.default().timer.clearTimeout(handle); } };
35 this._isCancelled = false;
38 if (!this._isCancelled) {
39 this._isCancelled = true;
41 this._emitter.fire(undefined);
46 get isCancellationRequested() {
47 return this._isCancelled;
49 get onCancellationRequested() {
50 if (this._isCancelled) {
54 this._emitter = new events_1.Emitter();
56 return this._emitter.event;
60 this._emitter.dispose();
61 this._emitter = undefined;
65 class CancellationTokenSource {
68 // be lazy and create the token only when
70 this._token = new MutableToken();
76 // save an object by returning the default
77 // cancelled token when cancellation happens
78 // before someone asks for the token
79 this._token = CancellationToken.Cancelled;
87 // ensure to initialize with an empty token if we had none
88 this._token = CancellationToken.None;
90 else if (this._token instanceof MutableToken) {
92 this._token.dispose();
96 exports.CancellationTokenSource = CancellationTokenSource;
97 //# sourceMappingURL=cancellation.js.map