@difizen/mana-common
Version:
153 lines (152 loc) • 6.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CancellationTokenSource = exports.CancellationToken = void 0;
exports.cancelled = cancelled;
exports.checkCancelled = checkCancelled;
exports.isCancelled = isCancelled;
var _event = require("./event");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*---------------------------------------------------------------------------------------------
* 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.
*--------------------------------------------------------------------------------------------*/
var shortcutEvent = Object.freeze(function (callback, context) {
var handle = setTimeout(callback.bind(context), 0);
return {
dispose: function dispose() {
clearTimeout(handle);
}
};
});
var CancellationToken;
(function (_CancellationToken) {
function is(thing) {
if (thing === CancellationToken.None || thing === CancellationToken.Cancelled) {
return true;
}
if (thing instanceof MutableToken) {
return true;
}
if (!thing || _typeof(thing) !== 'object') {
return false;
}
return typeof thing.isCancellationRequested === 'boolean' && typeof thing.onCancellationRequested === 'function';
}
_CancellationToken.is = is;
var None = _CancellationToken.None = Object.freeze({
isCancellationRequested: false,
onCancellationRequested: _event.Event.None
});
var Cancelled = _CancellationToken.Cancelled = Object.freeze({
isCancellationRequested: true,
onCancellationRequested: shortcutEvent
});
})(CancellationToken || (exports.CancellationToken = CancellationToken = {}));
var MutableToken = /*#__PURE__*/function () {
function MutableToken() {
_classCallCheck(this, MutableToken);
_defineProperty(this, "_isCancelled", false);
_defineProperty(this, "_emitter", void 0);
}
_createClass(MutableToken, [{
key: "cancel",
value: function cancel() {
if (!this._isCancelled) {
this._isCancelled = true;
if (this._emitter) {
this._emitter.fire(undefined);
this._emitter = undefined;
}
}
}
}, {
key: "isCancellationRequested",
get: function get() {
return this._isCancelled;
}
}, {
key: "onCancellationRequested",
get: function get() {
if (this._isCancelled) {
return shortcutEvent;
}
if (!this._emitter) {
this._emitter = new _event.Emitter();
}
return this._emitter.event;
}
}, {
key: "dispose",
value: function dispose() {
if (this._emitter) {
this._emitter.dispose();
}
}
}]);
return MutableToken;
}();
var CancellationTokenSource = exports.CancellationTokenSource = /*#__PURE__*/function () {
function CancellationTokenSource() {
_classCallCheck(this, CancellationTokenSource);
_defineProperty(this, "_token", void 0);
}
_createClass(CancellationTokenSource, [{
key: "token",
get: function get() {
if (!this._token) {
// be lazy and create the token only when
// actually needed
this._token = new MutableToken();
}
return this._token;
}
}, {
key: "cancel",
value: function 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 instanceof MutableToken) {
// actually cancel
this._token.cancel();
}
}
}, {
key: "dispose",
value: function dispose() {
var cancel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
if (cancel) {
this.cancel();
}
if (!this._token) {
// ensure to initialize with an empty token if we had none
this._token = CancellationToken.None;
} else if (this._token instanceof MutableToken) {
// actually dispose
this._token.dispose();
}
}
}]);
return CancellationTokenSource;
}();
var cancelledMessage = 'Cancelled';
function cancelled() {
return new Error(cancelledMessage);
}
function isCancelled(err) {
return !!err && err.message === cancelledMessage;
}
function checkCancelled(token) {
if (!!token && token.isCancellationRequested) {
throw cancelled();
}
}