UNPKG

@jovian/type-tools

Version:

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

186 lines 6.15 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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.HalfLifed = void 0; var ix_entity_1 = require("./ix.entity"); var HalfLifed = (function (_super) { __extends(HalfLifed, _super); function HalfLifed(init) { var _this = _super.call(this, 'halflifed') || this; _this.hl = 86400; _this.hlInvertedMs = 0; _this.last = Date.now(); _this.threshold = 0.000001; _this.ticker = null; _this.valueData = 0; _this.frozen = false; _this.afterUpdate = []; _this.afterEachTick = []; Object.assign(_this, init); if (_this.hl < 0.01) { _this.hl = 0.01; } _this.hlInvertedMs = 1 / (_this.hl * 1000); if (init.v !== undefined) { _this.value = init.v; } _this.addOnDestroy(function () { _this.unlinkTicker(); _this.afterUpdate.length = 0; _this.afterEachTick.length = 0; }); return _this; } Object.defineProperty(HalfLifed.prototype, "value", { get: function () { this.update(); return this.valueData; }, set: function (v) { this.valueData = v; this.last = Date.now(); }, enumerable: false, configurable: true }); HalfLifed.prototype.add = function (v) { if (!Number.isFinite(v) || v <= 0) { return this; } this.update(); this.valueData += v; this.trim(); return this.afterValueChange(); }; HalfLifed.prototype.sub = function (v) { if (!Number.isFinite(v) || v <= 0) { return this; } this.update(); this.valueData -= v; this.trim(); return this.afterValueChange(); }; HalfLifed.prototype.mul = function (v) { if (!Number.isFinite(v) || v < 0) { return this; } this.update(); this.valueData *= v; this.trim(); return this.afterValueChange(); }; HalfLifed.prototype.div = function (v) { if (!Number.isFinite(v) || v <= 0) { return this; } this.update(); this.valueData /= v; this.trim(); return this.afterValueChange(); }; HalfLifed.prototype.reset = function (v) { if (v === void 0) { v = 0; } this.value = v; this.trim(); return this.afterValueChange(); }; HalfLifed.prototype.afterValueChange = function () { for (var _i = 0, _a = this.afterUpdate; _i < _a.length; _i++) { var afterUpdateCb = _a[_i]; afterUpdateCb(this, this.valueData); } return this; }; HalfLifed.prototype.update = function () { if (this.frozen) { return this; } var now = Date.now(); if (this.valueData > 0) { var deltaMs = now - this.last; if (deltaMs > 0) { this.valueData = this.valueData * Math.pow(0.5, deltaMs * this.hlInvertedMs); this.trim(); } } this.last = now; return this; }; HalfLifed.prototype.linkTicker = function (callbackkMap) { this.unlinkTicker(); callbackkMap[this.ixId] = this; this.ticker = { type: 'linked', data: callbackkMap, }; return this; }; HalfLifed.prototype.trim = function () { if (this.valueData < this.threshold || !Number.isFinite(this.valueData)) { this.valueData = 0; } return this; }; HalfLifed.prototype.unlinkTicker = function () { if (!this.ticker) { return this; } if (this.ticker.type === 'default') { clearInterval(this.ticker.data); } else if (this.ticker.type === 'linked') { var callbackMap = this.ticker.data; if (callbackMap[this.ixId]) { delete callbackMap[this.ixId]; } } this.ticker = null; return this; }; HalfLifed.prototype.tick = function () { this.update(); if (this.afterEachTick.length > 0) { for (var _i = 0, _a = this.afterEachTick; _i < _a.length; _i++) { var afterEachTick = _a[_i]; afterEachTick(this, this.valueData); } } return this; }; HalfLifed.prototype.startTicker = function (type, intervalMs, afterEachTick) { var _this = this; if (type === void 0) { type = 'default'; } if (intervalMs === void 0) { intervalMs = 1000; } if (afterEachTick === void 0) { afterEachTick = null; } this.unlinkTicker(); if (!type) { type = 'default'; } if (type === 'default') { if (afterEachTick) { this.afterEachTick.push(afterEachTick); } this.ticker = { type: 'default', data: setInterval(function () { _this.tick(); }, intervalMs) }; } return this; }; HalfLifed.prototype.afterTick = function (afterTickCallback) { this.afterEachTick.push(afterTickCallback); return this; }; return HalfLifed; }(ix_entity_1.Entity)); exports.HalfLifed = HalfLifed; //# sourceMappingURL=ix.halflifed.js.map