evt
Version:
Type safe replacement for node's EventEmitter
155 lines • 5.69 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ctx = void 0;
// @denoify-line-ignore
var Set_1 = require("minimal-polyfills/Set");
// @denoify-line-ignore
var WeakMap_1 = require("minimal-polyfills/WeakMap");
var assert_1 = require("tsafe/assert");
var LazyEvt_1 = require("./LazyEvt");
var importProxy_1 = require("./importProxy");
var overwriteReadonlyProp_1 = require("tsafe/lab/overwriteReadonlyProp");
var CtxImpl = /** @class */ (function () {
function CtxImpl() {
this.lazyEvtAttach = new LazyEvt_1.LazyEvt();
this.lazyEvtDetach = new LazyEvt_1.LazyEvt();
this.lazyEvtDoneOrAborted = new LazyEvt_1.LazyEvt();
this.handlers = new Set_1.Polyfill();
this.evtByHandler = new WeakMap_1.Polyfill();
}
Object.defineProperty(CtxImpl.prototype, "evtDoneOrAborted", {
get: function () {
return this.lazyEvtDoneOrAborted.evt;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CtxImpl.prototype, "evtAttach", {
get: function () {
return this.lazyEvtAttach.evt;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CtxImpl.prototype, "evtDetach", {
get: function () {
return this.lazyEvtDetach.evt;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CtxImpl.prototype, "completionStatus", {
get: function () {
return this.__completionStatus;
},
enumerable: false,
configurable: true
});
CtxImpl.prototype.onDoneOrAborted = function (doneOrAborted) {
this.__completionStatus = doneOrAborted;
this.lazyEvtDoneOrAborted.post(doneOrAborted);
};
CtxImpl.prototype.waitFor = function (timeout) {
var _this_1 = this;
return this.evtDoneOrAborted
.waitFor(timeout)
.then(function (data) {
if (data.type === "ABORTED") {
throw data.error;
}
return data.result;
}, function (timeoutError) {
_this_1.abort(timeoutError);
throw timeoutError;
});
};
CtxImpl.prototype.abort = function (error) {
return this.__done(error);
};
CtxImpl.prototype.done = function (result) {
return this.__done(undefined, result);
};
/** Detach all handler bound to this context from theirs respective Evt and post getEvtDone() */
CtxImpl.prototype.__done = function (error, result) {
var e_1, _a;
var handlers = [];
try {
for (var _b = __values(this.handlers.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
var handler = _c.value;
var evt = this.evtByHandler.get(handler);
var wasStillAttached = handler.detach();
//NOTE: It should not be possible
if (!wasStillAttached) {
continue;
}
handlers.push({ handler: handler, evt: evt });
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
this.onDoneOrAborted(__assign(__assign({}, (!!error ?
{ type: "ABORTED", error: error } :
{ type: "DONE", "result": result })), { handlers: handlers }));
return handlers;
};
CtxImpl.prototype.getHandlers = function () {
var _this_1 = this;
return Array.from(this.handlers.values())
.map(function (handler) { return ({ handler: handler, "evt": _this_1.evtByHandler.get(handler) }); });
};
CtxImpl.prototype.zz__addHandler = function (handler, evt) {
(0, assert_1.assert)(handler.ctx === this);
(0, assert_1.assert)((0, assert_1.is)(handler));
if (this.completionStatus !== undefined) {
handler.detach();
return;
}
this.handlers.add(handler);
this.evtByHandler.set(handler, evt);
this.lazyEvtAttach.post({ handler: handler, evt: evt });
};
CtxImpl.prototype.zz__removeHandler = function (handler) {
(0, assert_1.assert)(handler.ctx === this);
(0, assert_1.assert)((0, assert_1.is)(handler));
this.lazyEvtDetach.post({
handler: handler,
"evt": this.evtByHandler.get(handler)
});
this.handlers.delete(handler);
};
return CtxImpl;
}());
exports.Ctx = CtxImpl;
try {
(0, overwriteReadonlyProp_1.overwriteReadonlyProp)(exports.Ctx, "name", "Ctx");
}
catch (_a) { }
importProxy_1.importProxy.Ctx = exports.Ctx;
//# sourceMappingURL=Ctx.js.map