UNPKG

@freemework/common

Version:

Common library of the Freemework Project.

61 lines 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FCancellationTokenSourceManual = void 0; const index_js_1 = require("../exception/index.js"); const f_cancellation_exception_js_1 = require("./f_cancellation_exception.js"); class FCancellationTokenSourceManual { _token; _cancelListeners = []; _isCancellationRequested; constructor() { this._isCancellationRequested = false; const self = this; this._token = { get isCancellationRequested() { return self.isCancellationRequested; }, addCancelListener(cb) { self.addCancelListener(cb); }, removeCancelListener(cb) { self.removeCancelListener(cb); }, throwIfCancellationRequested() { self.throwIfCancellationRequested(); } }; } get token() { return this._token; } get isCancellationRequested() { return this._isCancellationRequested; } cancel() { if (this._isCancellationRequested) { // Prevent to call listeners twice return; } this._isCancellationRequested = true; const errors = []; if (this._cancelListeners.length > 0) { // Release callback. We do not need its anymore const cancelListeners = this._cancelListeners.splice(0); cancelListeners.forEach(cancelListener => { try { cancelListener(); } catch (e) { errors.push(index_js_1.FException.wrapIfNeeded(e)); } }); } if (errors.length > 0) { throw new index_js_1.FExceptionAggregate(errors); } } addCancelListener(cb) { this._cancelListeners.push(cb); } removeCancelListener(cb) { const cbIndex = this._cancelListeners.indexOf(cb); if (cbIndex !== -1) { this._cancelListeners.splice(cbIndex, 1); } } throwIfCancellationRequested() { if (this.isCancellationRequested) { throw new f_cancellation_exception_js_1.FCancellationException(); } } } exports.FCancellationTokenSourceManual = FCancellationTokenSourceManual; //# sourceMappingURL=f_cancellation_token_source_manual.js.map