UNPKG

@jovian/type-tools

Version:

TypeTools is a Typescript library for providing extensible tooling runtime validations and type helpers.

239 lines 12.8 kB
"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 __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReconnEntity = exports.ReconnEntityBehavior = void 0; var ix_entity_1 = require("./ix.entity"); var ix_halflifed_1 = require("./ix.halflifed"); var ix_timer_1 = require("./ix.timer"); var ReconnEntityBehavior = (function () { function ReconnEntityBehavior(init) { this.resetThreshold = 5; this.defunctThreshold = 3; this.errorHalfLife = 300; this.defunctHalfLife = 3600; this.restoreCheckInterval = 60; if (init) { Object.assign(this, init); } } ReconnEntityBehavior.defaultProfile = null; return ReconnEntityBehavior; }()); exports.ReconnEntityBehavior = ReconnEntityBehavior; var ReconnEntity = (function (_super) { __extends(ReconnEntity, _super); function ReconnEntity(entityType, ixIdOverride) { var _this = _super.call(this, entityType, ixIdOverride) || this; _this.ixReconn = { config: { resetThreshold: 5, defunctThreshold: 3, }, configSource: null, configSubs: null, resetInProgress: null, error: null, errorLast: 0, errorHeat: new ix_halflifed_1.HalfLifed({ hl: 300 }), defunctHeat: new ix_halflifed_1.HalfLifed({ hl: 3600 }), defunctState: false, defunctRestoreCheckedLast: 0, defunctRestoreCheckInterval: 60, defunctRestoreChecker: null, setRestoreFunction: function (attempt) { _this.ixReconn.actions.attemptToRestore = attempt; }, setConfigSource: function (src) { if (_this.ixReconn.configSource === src) { return; } if (_this.ixReconn.configSubs) { _this.ixReconn.configSubs.unsubscribe(); } _this.ixReconn.configSource = src; _this.ixReconn.configSubs = src.change$.subscribe(function (confData) { var conf = new ReconnEntityBehavior(confData); _this.ixReconn.config.resetThreshold = conf.resetThreshold; _this.ixReconn.config.defunctThreshold = conf.defunctThreshold; _this.ixReconn.errorHeat.hl = conf.errorHalfLife; _this.ixReconn.defunctHeat.hl = conf.defunctHalfLife; _this.ixReconn.defunctRestoreCheckInterval = conf.restoreCheckInterval; _this.ixRx('reconn:config_change').next(_this); }); }, actions: { reset: null, attemptToRestore: null, }, on: { reset: null, defunct: null, restore: null, }, event: { self: _this, get errorHeatUp$() { return this.self.ixRx('reconn:error_heat_up').obs(); }, get defunctHeatUp$() { return this.self.ixRx('reconn:defunct_heat_up').obs(); }, get errorDuringDefunct$() { return this.self.ixRx('reconn:error_during_defunct').obs(); }, get beforeReset$() { return this.self.ixRx('reconn:before_reset').obs(); }, get reset$() { return this.self.ixRx('reconn:reset').obs(); }, get beforeDefunct$() { return this.self.ixRx('reconn:before_defunct').obs(); }, get defunct$() { return this.self.ixRx('reconn:defunct').obs(); }, get beforeRestore$() { return this.self.ixRx('reconn:before_restore').obs(); }, get restore$() { return this.self.ixRx('reconn:restore').obs(); }, get configChange$() { return this.self.ixRx('reconn:config_change').obs(); }, } }; if (!ReconnEntityBehavior.defaultProfile) { ReconnEntityBehavior.defaultProfile = new ReconnEntityBehavior(); } var dfProfile = ReconnEntityBehavior.defaultProfile; _this.ixReconn.config.resetThreshold = dfProfile.resetThreshold; _this.ixReconn.config.defunctThreshold = dfProfile.defunctThreshold; _this.ixReconn.errorHeat.hl = dfProfile.errorHalfLife; _this.ixReconn.defunctHeat.hl = dfProfile.defunctHalfLife; _this.ixReconn.defunctRestoreCheckInterval = dfProfile.restoreCheckInterval; _this.ixReconn.errorHeat.afterUpdate.push(function (heat, v) { return __awaiter(_this, void 0, void 0, function () { var resolver_1, e_1; var _a, _b, _c, _d, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: if (this.ixReconn.defunctState) { this.ixRx('reconn:error_during_defunct').next(this.ixReconn.error); return [2]; } if (!(this.ixReconn.errorHeat.value > this.ixReconn.config.resetThreshold)) return [3, 6]; this.ixReconn.errorHeat.reset(); this.ixReconn.defunctHeat.add(1); this.ixRx('reconn:defunct_heat_up').next(this.ixReconn.defunctHeat.value); if (!(this.ixReconn.defunctHeat.value < this.ixReconn.config.defunctThreshold)) return [3, 5]; this.ixRx('reconn:before_reset').next(this); resolver_1 = null; this.ixReconn.resetInProgress = new Promise(function (resolve) { return resolver_1 = resolve; }); _g.label = 1; case 1: _g.trys.push([1, 3, , 4]); return [4, Promise.resolve((_b = (_a = this.ixReconn.actions).reset) === null || _b === void 0 ? void 0 : _b.call(_a))]; case 2: _g.sent(); return [3, 4]; case 3: e_1 = _g.sent(); this.ixError(e_1); return [3, 4]; case 4: this.ixReconn.resetInProgress = null; resolver_1 === null || resolver_1 === void 0 ? void 0 : resolver_1(); (_d = (_c = this.ixReconn.on).reset) === null || _d === void 0 ? void 0 : _d.call(_c); this.ixRx('reconn:reset').next(this); return [3, 6]; case 5: this.ixReconn.defunctHeat.reset(); this.ixReconn.defunctState = true; this.ixReconn.defunctRestoreCheckedLast = Date.now(); this.ixRx('reconn:before_defunct').next(this); (_f = (_e = this.ixReconn.on).defunct) === null || _f === void 0 ? void 0 : _f.call(_e); this.ixRx('reconn:defunct').next(this); _g.label = 6; case 6: return [2]; } }); }); }); _this.ixReconn.defunctRestoreChecker = (0, ix_timer_1.timerInterval)(1000, function () { return __awaiter(_this, void 0, void 0, function () { var res; var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!this.ixReconn.defunctState) { return [2]; } if (!(Date.now() - this.ixReconn.defunctRestoreCheckedLast > this.ixReconn.defunctRestoreCheckInterval * 1000)) return [3, 2]; this.ixReconn.defunctRestoreCheckedLast = Date.now(); if (!this.ixReconn.actions.attemptToRestore) return [3, 2]; return [4, Promise.resolve(this.ixReconn.actions.attemptToRestore(this))]; case 1: res = _c.sent(); if (res) { this.ixReconn.defunctState = false; this.ixRx('reconn:before_restore').next(this); this.ixReconn.errorHeat.reset(); this.ixReconn.defunctHeat.reset(); (_b = (_a = this.ixReconn.on).restore) === null || _b === void 0 ? void 0 : _b.call(_a); this.ixRx('reconn:restore').next(this); } _c.label = 2; case 2: return [2]; } }); }); }); var eSub = _this.error$.subscribe(function (e) { var severity = e.severity ? e.severity : 1; _this.ixReconn.errorLast = Date.now(); _this.ixReconn.error = e; _this.ixReconn.errorHeat.add(severity); _this.ixRx('reconn:error_heat_up').next(_this.ixReconn.errorHeat.value); }); _this.ixReconn.errorHeat.lcManagedBy(_this); _this.ixReconn.defunctHeat.lcManagedBy(_this); _this.ixReconn.defunctRestoreChecker.lcManagedBy(_this); _this.addOnDestroy(function () { eSub.unsubscribe(); if (_this.ixReconn.configSubs) { _this.ixReconn.configSubs.unsubscribe(); } }); return _this; } return ReconnEntity; }(ix_entity_1.Entity)); exports.ReconnEntity = ReconnEntity; //# sourceMappingURL=ix.entity.reconn.js.map