@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
60 lines • 2.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CancellationToken = void 0;
class CancellationToken {
_isCancelled = false;
_isDisposed = false;
request;
callbacks = [];
cancellationListener;
constructor(request) {
if (request) {
this.request = request;
this.cancellationListener = this.cancel.bind(this);
request.once('close', this.cancellationListener);
request.once('end', this.cancellationListener);
request.once('aborted', this.cancellationListener);
// NOTE:
// Cancelling a Request through "fetch" or "cross-fetch" doesn't cancel the http request, but simply closes the underlying connection entirely.
// Tools like Postman behave the same way.
// This behavior works around Request Cancellation, which results in the cancellation token never being triggered.
// We must enforce cancellation here, or risk long polling timeouts being executed long after the request was terminated.
request.socket.once('close', this.cancellationListener);
request.socket.once('end', this.cancellationListener);
request.socket.once('error', this.cancellationListener);
}
}
get isCancelled() {
return this._isCancelled;
}
dispose() {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this.callbacks = [];
if (this.request) {
this.request.removeListener('close', this.cancellationListener);
this.request.removeListener('end', this.cancellationListener);
this.request.removeListener('aborted', this.cancellationListener);
this.request.socket.removeListener('close', this.cancellationListener);
this.request.socket.removeListener('end', this.cancellationListener);
this.request.socket.removeListener('error', this.cancellationListener);
}
}
onCancellation(callback) {
this.callbacks.push(callback);
}
async cancel() {
if (this.isCancelled) {
return;
}
this._isCancelled = true;
for (const callback of this.callbacks) {
await callback();
}
this.dispose();
}
}
exports.CancellationToken = CancellationToken;
//# sourceMappingURL=CancellationToken.js.map