@jovian/type-tools
Version:
TypeTools is a Typescript library for providing extensible tooling runtime validations and type helpers.
486 lines • 18.3 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MajorScope = exports.Scope = exports.Registry = exports.Entity = exports.LinkedObservable = void 0;
var rxjs_1 = require("rxjs");
var ix_error_1 = require("./ix.error");
var ix_event_1 = require("./ix.event");
var prom_util_1 = require("./util/prom.util");
var commonEntityIdData = { index: 0 };
var commonEntityIdGeneration = {
type: 'default',
generator: function (entityType) {
var idx = (commonEntityIdData.index++).toString(36);
return "".concat(entityType, "-").concat(idx);
},
changeHistory: [],
};
var LinkedObservable = (function (_super) {
__extends(LinkedObservable, _super);
function LinkedObservable() {
return _super.call(this) || this;
}
LinkedObservable.fromLinkedSubject = function (subj) {
var obs = subj.asObservable();
obs.subject = subj;
obs.dataSourceEntity = subj.__rx_data_source_entity;
var rxOpts = subj.__rx_opts;
if (rxOpts) {
if (rxOpts.trackSubscribers) {
Object.defineProperty(obs, 'subscribe2', { value: obs.subscribe });
obs.subscribe = function (listener) {
var entity = listener.__rx_data_source_entity;
if (rxOpts.onNewSubscriber) {
rxOpts.onNewSubscriber(new Error, entity);
}
obs.subscribe2(listener);
};
}
}
return obs;
};
LinkedObservable.prototype.next = function (nextValue, errorHandler) {
try {
var sub = this.subject;
sub.next(nextValue);
}
catch (e) {
try {
if (errorHandler) {
errorHandler(e);
}
}
catch (e2) { }
}
};
return LinkedObservable;
}(rxjs_1.Observable));
exports.LinkedObservable = LinkedObservable;
var Entity = (function () {
function Entity(entityType, ixIdOverride) {
this.ixChildrenMapV = {};
this.ixRegistriesV = {};
this.ixRxObjectsV = {};
this.ixOnDestoys = [];
this.ixDestroyed = false;
this.ixIdV = ixIdOverride ? ixIdOverride : commonEntityIdGeneration.generator(entityType);
}
Entity.idGenType = function () { return commonEntityIdGeneration.type; };
Entity.idGenChangeHistory = function () { return commonEntityIdGeneration.changeHistory.slice(0); };
Entity.idGenCustom = function (type, generator) {
commonEntityIdGeneration.type = type;
commonEntityIdGeneration.generator = generator;
commonEntityIdGeneration.changeHistory.push(new Error());
};
Object.defineProperty(Entity.prototype, "ixId", {
get: function () { return this.ixIdV; },
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "ixParent", {
get: function () { return this.ixParentV; },
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "ixChildrenMap", {
get: function () { return this.ixChildrenMapV; },
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "ixRegistries", {
get: function () { return this.ixRegistriesV; },
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "ixMajorScope", {
get: function () { return this.ixMajorScopeV; },
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "destroyed", {
get: function () { return this.ixDestroyed; },
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "ixBase", {
get: function () { return this.ixMajorScopeV ? this.ixMajorScopeV.name : "unknown"; },
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "error$", {
get: function () {
var _this = this;
var rxObj = this.ixRx('error');
if (!rxObj.subject) {
rxObj.init();
}
if (!rxObj.data.init) {
rxObj.data.init = true;
var gSub_1 = ix_error_1.error$.subscribe(function (e) {
if (e.entity === _this || (e.appliesTo && e.appliesTo[_this.ixId])) {
rxObj.next(e);
}
});
this.addOnDestroy(function () { gSub_1.unsubscribe(); });
}
return rxObj.observable;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "event$", {
get: function () {
var _this = this;
var rxObj = this.ixRx('event');
if (!rxObj.subject) {
rxObj.init();
}
if (!rxObj.data.init) {
rxObj.data.init = true;
var gSub_2 = ix_event_1.event$.subscribe(function (e) {
if (e.entity === _this || (e.appliesTo && e.appliesTo[_this.ixId])) {
rxObj.next(e);
}
});
this.addOnDestroy(function () { gSub_2.unsubscribe(); });
}
return rxObj.observable;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Entity.prototype, "any$", {
get: function () {
var rxObj = this.ixRx('__any');
if (!rxObj.subject) {
rxObj.init();
}
return rxObj.observable;
},
enumerable: false,
configurable: true
});
Entity.prototype.ixRegisterOn = function (registry) {
registry.register(this);
return this;
};
Entity.prototype.ixSetEntityId = function (id) {
var _a;
if (id === this.ixIdV) {
return this;
}
var oldId = this.ixIdV;
var registries = [];
for (var _i = 0, _b = Object.keys(this.ixRegistries); _i < _b.length; _i++) {
var regId = _b[_i];
var registry = this.ixRegistries[regId];
var t = registry.registrationTime(this);
registries.push({ registry: registry, t: t });
registry.deregister(this);
}
this.ixIdV = id;
if ((_a = this.ixParentV) === null || _a === void 0 ? void 0 : _a.ixChildrenMapV[oldId]) {
delete this.ixParentV.ixChildrenMapV[oldId];
this.ixParentV.ixChildrenMapV[id] = this;
}
for (var _c = 0, registries_1 = registries; _c < registries_1.length; _c++) {
var regInfo = registries_1[_c];
regInfo.registry.register(this, regInfo.t);
}
return this;
};
Entity.prototype.ixError = function (e, severity, metadata) {
if (typeof e === 'string') {
e = new Error(e);
}
var se = (0, ix_error_1.enscope)(e, this);
if (!severity) {
severity = 1;
}
if (metadata) {
se.meta = metadata;
}
se.severity = severity;
if (!se.appliesTo) {
se.appliesTo = {};
}
se.appliesTo[this.ixId] = this;
(0, ix_error_1.errorCast)(se);
return this;
};
Entity.prototype.ixEvent = function (evtName, evtData) {
(0, ix_event_1.eventCast)(evtName, evtData, this);
return this;
};
Entity.prototype.ixListen = function (obs, listener) {
Object.defineProperty(listener, '__rx_data_source_entity', { value: this });
var subs = typeof listener === 'function' ? obs.subscribe(listener) : obs.subscribe(listener);
this.addOnDestroy(function () { subs.unsubscribe(); });
return this;
};
Entity.prototype.lcManagedBy = function (parent) {
if (!parent) {
return this;
}
this.lcDetach(true);
parent.ixChildrenMapV[this.ixIdV] = this;
this.ixParentV = parent;
this.lcSetMajorScope(parent.ixMajorScopeV);
return this;
};
Entity.prototype.lcManage = function (child) {
if (!child) {
return this;
}
child.lcDetach(true);
this.ixChildrenMapV[child.ixId] = child;
child.ixParentV = this;
child.lcSetMajorScope(this.ixMajorScopeV);
return this;
};
Entity.prototype.lcDetach = function (skipRootDetach) {
if (skipRootDetach === void 0) { skipRootDetach = false; }
if (this.ixParentV) {
delete this.ixParentV.ixChildrenMapV[this.ixIdV];
this.ixParentV = null;
if (!skipRootDetach) {
this.lcSetMajorScope(null);
}
}
};
Entity.prototype.lcSetMajorScope = function (rootScope, alsoUpdateChildren) {
if (alsoUpdateChildren === void 0) { alsoUpdateChildren = true; }
this.ixMajorScopeV = rootScope;
if (alsoUpdateChildren) {
for (var _i = 0, _a = Object.keys(this.ixChildrenMapV); _i < _a.length; _i++) {
var childId = _a[_i];
var child = this.ixChildrenMapV[childId];
if (child) {
child.lcSetMajorScope(rootScope);
}
}
}
return this;
};
Entity.prototype.ixRx = function (rxName, rcOpts) {
var _this = this;
if ((rcOpts === null || rcOpts === void 0 ? void 0 : rcOpts.findNameFromThisObservable) && rcOpts.findNameFromThisObservable.__rx_name) {
rxName = rcOpts.findNameFromThisObservable.__rx_name;
}
if (!rxName) {
return null;
}
var rxObj = this.ixRxObjectsV[rxName];
if (!rxObj) {
rxObj = this.ixRxObjectsV[rxName] = {
name: rxName,
data: {},
subject: null,
observable: null,
oninits: [],
init: function () {
if (rxObj.subject) {
return;
}
rxObj.subject = new rxjs_1.Subject();
Object.defineProperty(rxObj.subject, '__rx_data_source_entity', { value: rxName });
Object.defineProperty(rxObj.subject, '__rx_opts', { value: rxName });
rxObj.observable = LinkedObservable.fromLinkedSubject(rxObj.subject);
Object.defineProperty(rxObj.observable, '__rx_name', { value: rxName });
Object.defineProperty(rxObj.observable, 'next', { value: function (nextValue, errorHandler) {
try {
var sub = rxObj.subject;
sub.next(nextValue);
}
catch (e) {
try {
if (errorHandler) {
errorHandler(e);
}
}
catch (e2) { }
}
} });
for (var _i = 0, _a = rxObj.oninits; _i < _a.length; _i++) {
var oninit = _a[_i];
oninit();
}
},
obs: function () {
if (!rxObj.subject) {
rxObj.init();
}
return rxObj.observable;
},
next: function (v) {
if (!rxObj.subject) {
rxObj.init();
}
if (rxName !== '__any') {
var genericSelfRx = _this.ixRx('__any');
if (!genericSelfRx.subject) {
genericSelfRx.init();
}
genericSelfRx.next(v);
}
if (rxObj.subject) {
rxObj.subject.next(v);
}
}
};
}
return rxObj;
};
Entity.prototype.addOnDestroy = function (ondestroy) {
this.ixOnDestoys.push(ondestroy);
return this;
};
Entity.prototype.destroy = function () {
var _a;
if (this.ixDestroyed) {
return Promise.resolve(null);
}
this.ixDestroyed = true;
var subDestroyProms = [];
if (this.ixOnDestoys) {
for (var _i = 0, _b = this.ixOnDestoys; _i < _b.length; _i++) {
var ixOnDestroy = _b[_i];
if (ixOnDestroy) {
try {
var v = ixOnDestroy(this);
if (v && v.then) {
subDestroyProms.push(v);
}
}
catch (e) {
(0, ix_error_1.errorCast)(e);
}
}
}
}
for (var _c = 0, _d = Object.keys(this.ixRegistries); _c < _d.length; _c++) {
var regId = _d[_c];
this.ixRegistries[regId].deregister(this);
}
for (var _e = 0, _f = Object.keys(this.ixChildrenMapV); _e < _f.length; _e++) {
var childId = _f[_e];
var child = this.ixChildrenMapV[childId];
if (child) {
subDestroyProms.push(child.destroy());
}
}
this.lcDetach();
for (var _g = 0, _h = Object.keys(this.ixRxObjectsV); _g < _h.length; _g++) {
var rxName = _h[_g];
var rxReg = this.ixRxObjectsV[rxName];
(_a = rxReg.subject) === null || _a === void 0 ? void 0 : _a.complete();
}
this.ixRxObjectsV = null;
this.ixMajorScopeV = null;
this.ixChildrenMapV = null;
return prom_util_1.PromUtil.allSettled(subDestroyProms);
};
return Entity;
}());
exports.Entity = Entity;
var Registry = (function (_super) {
__extends(Registry, _super);
function Registry() {
var _this = _super.call(this, 'registry') || this;
_this.data = {};
return _this;
}
Registry.prototype.ixSetEntityId = function (id) {
if (id) {
var e = Error('Registry type cannot change its ixId');
this.ixError(e);
throw e;
}
return this;
};
Registry.prototype.get = function (ixId) { return this.data[ixId].target; };
Registry.prototype.has = function (entity) { return this.data[entity.ixId] ? true : false; };
Registry.prototype.exists = function (entity) { return this.data[entity.ixId] ? true : false; };
Registry.prototype.register = function (entity, overrideTime) {
var entityIxId = entity.ixId;
if (this.data[entityIxId]) {
return this;
}
this.data[entityIxId] = { target: entity, t: overrideTime ? overrideTime : Date.now() };
entity.ixRegistries[this.ixId] = this;
return this;
};
Registry.prototype.deregister = function (entity) {
var reg = this.data[entity.ixId];
if (reg) {
delete reg.target.ixRegistries[this.ixId];
delete this.data[entity.ixId];
}
return this;
};
Registry.prototype.registrationTime = function (entity) {
var reg = this.data[entity.ixId];
return reg ? reg.t : null;
};
return Registry;
}(Entity));
exports.Registry = Registry;
var Scope = (function (_super) {
__extends(Scope, _super);
function Scope(name, executeWithinScope, isMajorScope) {
var _this = _super.call(this, isMajorScope ? 'major-scope' : 'scope', null) || this;
_this.name = name;
_this.isMajorScope = !!isMajorScope;
if (!_this.name) {
_this.name = isMajorScope ? "(major-scope-".concat(_this.ixId, ")") : "(scope-".concat(_this.ixId, ")");
}
_this.executeWithinScope = executeWithinScope;
if (_this.executeWithinScope) {
_this.executeWithinScope(_this);
}
return _this;
}
return Scope;
}(Entity));
exports.Scope = Scope;
var MajorScope = (function (_super) {
__extends(MajorScope, _super);
function MajorScope(name, executeWithinScope) {
var _this = _super.call(this, name, executeWithinScope, true) || this;
_this.lcSetMajorScope(_this);
_this.ixRx('error').oninits.push(function () {
var scopedErrorSubs = ix_error_1.error$.subscribe(function (e) {
if (e.scope === _this) {
_this.ixRx('error').next(e);
}
});
_this.addOnDestroy(function () { scopedErrorSubs.unsubscribe(); });
});
_this.ixRx('event').oninits.push(function () {
var scopedEventSubs = ix_event_1.event$.subscribe(function (e) {
if (e.scope === _this) {
_this.ixRx('event').next(e);
}
});
_this.addOnDestroy(function () { scopedEventSubs.unsubscribe(); });
});
return _this;
}
return MajorScope;
}(Scope));
exports.MajorScope = MajorScope;
//# sourceMappingURL=ix.entity.js.map