UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

123 lines 4.62 kB
"use strict"; // ***************************************************************************** // Copyright (C) 2017 TypeFox and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 // ***************************************************************************** /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation and others. All rights reserved. * Licensed under the MIT License. See https://github.com/Microsoft/vscode/blob/master/LICENSE.txt for license information. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.checkCancelled = exports.isCancelled = exports.cancelled = exports.CancellationTokenSource = exports.CancellationError = exports.CancellationToken = void 0; const event_1 = require("./event"); const types_1 = require("./types"); // eslint-disable-next-line @typescript-eslint/no-explicit-any const shortcutEvent = Object.freeze(Object.assign(function (callback, context) { const handle = setTimeout(callback.bind(context), 0); return { dispose() { clearTimeout(handle); } }; }, { get maxListeners() { return 0; }, set maxListeners(maxListeners) { } })); var CancellationToken; (function (CancellationToken) { CancellationToken.None = Object.freeze({ isCancellationRequested: false, onCancellationRequested: event_1.Event.None }); CancellationToken.Cancelled = Object.freeze({ isCancellationRequested: true, onCancellationRequested: shortcutEvent }); function is(value) { return (0, types_1.isObject)(value) && (value === CancellationToken.None || value === CancellationToken.Cancelled || ((0, types_1.isBoolean)(value.isCancellationRequested) && !!value.onCancellationRequested)); } CancellationToken.is = is; })(CancellationToken = exports.CancellationToken || (exports.CancellationToken = {})); class CancellationError extends Error { constructor() { super('Canceled'); this.name = this.message; } } exports.CancellationError = CancellationError; class MutableToken { constructor() { this._isCancelled = false; } cancel() { if (!this._isCancelled) { this._isCancelled = true; if (this._emitter) { this._emitter.fire(undefined); this._emitter = undefined; } } } get isCancellationRequested() { return this._isCancelled; } get onCancellationRequested() { if (this._isCancelled) { return shortcutEvent; } if (!this._emitter) { this._emitter = new event_1.Emitter(); } return this._emitter.event; } } class CancellationTokenSource { get token() { if (!this._token) { // be lazy and create the token only when // actually needed this._token = new MutableToken(); } return this._token; } cancel() { if (!this._token) { // save an object by returning the default // cancelled token when cancellation happens // before someone asks for the token this._token = CancellationToken.Cancelled; } else if (this._token !== CancellationToken.Cancelled) { this._token.cancel(); } } dispose() { this.cancel(); } } exports.CancellationTokenSource = CancellationTokenSource; const cancelledMessage = 'Cancelled'; function cancelled() { return new Error(cancelledMessage); } exports.cancelled = cancelled; function isCancelled(err) { return !!err && err.message === cancelledMessage; } exports.isCancelled = isCancelled; function checkCancelled(token) { if (!!token && token.isCancellationRequested) { throw cancelled(); } } exports.checkCancelled = checkCancelled; //# sourceMappingURL=cancellation.js.map