UNPKG

@trap_stevo/timetide

Version:

Master the flow of time with the ultimate solution for precision scheduling, real-time synchronization, and temporal control. Designed for developers who demand flexibility and power, this system blends intuitive time parsing, dynamic countdowns, and pers

321 lines (320 loc) 13.2 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var _TimeTide; function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } var _require = require("events"), EventEmitter = _require.EventEmitter; var path = require("path"); var fs = require("fs"); var TimeUtilitiesManager = require("./HUDManagers/TimeUtilitiesManager.js"); var TimeTide = /*#__PURE__*/function () { function TimeTide() { _classCallCheck(this, TimeTide); } return _createClass(TimeTide, null, [{ key: "configure", value: function configure() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; if (options.persistPath) { _assertClassBrand(TimeTide, this, _config)._.persistPath = options.persistPath; } if (options.disablePersistence !== undefined) { _assertClassBrand(TimeTide, this, _config)._.disablePersistence = !!options.disablePersistence; } } }, { key: "loadTimers", value: function loadTimers() { if (_assertClassBrand(TimeTide, this, _config)._.disablePersistence || !fs.existsSync(_assertClassBrand(TimeTide, this, _config)._.persistPath)) { return; } try { _persistedTasks._ = _assertClassBrand(TimeTide, this, JSON.parse(fs.readFileSync(_assertClassBrand(TimeTide, this, _config)._.persistPath))); for (var name in _assertClassBrand(TimeTide, this, _persistedTasks)._) { var task = _assertClassBrand(TimeTide, this, _persistedTasks)._[name]; var elapsed = Date.now() - task.createdAt; var remaining = Math.max(task.remainingMs - elapsed, 0); if (remaining > 0 && _assertClassBrand(TimeTide, this, _namedCallbacks)._[name]) { _assertClassBrand(TimeTide, this, _scheduleInternal).call(this, name, remaining, _assertClassBrand(TimeTide, this, _namedCallbacks)._[name]); } } } catch (error) { if (typeof _assertClassBrand(TimeTide, this, _config)._.onLoadTimersError === "function") { _assertClassBrand(TimeTide, this, _config)._.onLoadTimersError(error); } } } }, { key: "schedule", value: function schedule(nameOrMs, timeOrCallback, cbIfNamed) { var meta = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; _assertClassBrand(TimeTide, this, _ensureTimersInitialized).call(this); if (typeof nameOrMs === "string" && cbIfNamed) { var name = nameOrMs; var ms = this.toMilliseconds(timeOrCallback); _assertClassBrand(TimeTide, this, _namedCallbacks)._[name] = cbIfNamed; _assertClassBrand(TimeTide, this, _persistedTasks)._[name] = { remainingMs: ms, createdAt: Date.now(), meta: meta }; _assertClassBrand(TimeTide, this, _saveTimers).call(this); return _assertClassBrand(TimeTide, this, _scheduleInternal).call(this, name, ms, cbIfNamed); } else { var _ms = typeof nameOrMs === "number" ? nameOrMs : this.toMilliseconds(nameOrMs); return _assertClassBrand(TimeTide, this, _scheduleInternal).call(this, null, _ms, timeOrCallback); } } }, { key: "clear", value: function clear(id) { if (_assertClassBrand(TimeTide, this, _tasks)._[id]) { clearTimeout(_assertClassBrand(TimeTide, this, _tasks)._[id]); clearInterval(_assertClassBrand(TimeTide, this, _tasks)._[id]); delete _assertClassBrand(TimeTide, this, _tasks)._[id]; } if (_assertClassBrand(TimeTide, this, _persistedTasks)._[id]) { delete _assertClassBrand(TimeTide, this, _persistedTasks)._[id]; _assertClassBrand(TimeTide, this, _saveTimers).call(this); } } }, { key: "pause", value: function pause(name) { var task = _assertClassBrand(TimeTide, this, _persistedTasks)._[name]; if (!task) { return false; } var elapsed = Date.now() - task.createdAt; task.remainingMs = Math.max(0, task.remainingMs - elapsed); task.createdAt = Date.now(); this.clear(name); _assertClassBrand(TimeTide, this, _persistedTasks)._[name] = task; _assertClassBrand(TimeTide, this, _saveTimers).call(this); return true; } }, { key: "resume", value: function resume(name) { _assertClassBrand(TimeTide, this, _ensureTimersInitialized).call(this); var task = _assertClassBrand(TimeTide, this, _persistedTasks)._[name]; var cb = _assertClassBrand(TimeTide, this, _namedCallbacks)._[name]; if (!task || !cb) { return false; } return _assertClassBrand(TimeTide, this, _scheduleInternal).call(this, name, task.remainingMs, cb); } }, { key: "pauseAll", value: function pauseAll() { for (var name in _assertClassBrand(TimeTide, this, _persistedTasks)._) { this.pause(name); } } }, { key: "resumeAll", value: function resumeAll() { _assertClassBrand(TimeTide, this, _ensureTimersInitialized).call(this); for (var name in _assertClassBrand(TimeTide, this, _persistedTasks)._) { this.resume(name); } } }, { key: "then", value: function then(name, callback) { _assertClassBrand(TimeTide, this, _chainedTasks)._[name] = callback; } }, { key: "extend", value: function extend(name, dur) { if (!_assertClassBrand(TimeTide, this, _persistedTasks)._[name]) { return false; } _assertClassBrand(TimeTide, this, _persistedTasks)._[name].remainingMs += this.toMilliseconds(dur); _assertClassBrand(TimeTide, this, _saveTimers).call(this); return true; } }, { key: "reduce", value: function reduce(name, dur) { if (!_assertClassBrand(TimeTide, this, _persistedTasks)._[name]) { return false; } _assertClassBrand(TimeTide, this, _persistedTasks)._[name].remainingMs = Math.max(0, _assertClassBrand(TimeTide, this, _persistedTasks)._[name].remainingMs - this.toMilliseconds(dur)); _assertClassBrand(TimeTide, this, _saveTimers).call(this); return true; } }, { key: "reschedule", value: function reschedule(name, dur) { var cb = _assertClassBrand(TimeTide, this, _namedCallbacks)._[name]; if (!_assertClassBrand(TimeTide, this, _persistedTasks)._[name] || !cb) { return false; } this.clear(name); return this.schedule(name, dur, cb, _assertClassBrand(TimeTide, this, _persistedTasks)._[name].meta); } }, { key: "getTimers", value: function getTimers() { var now = Date.now(); var result = {}; for (var name in _assertClassBrand(TimeTide, this, _persistedTasks)._) { var task = _assertClassBrand(TimeTide, this, _persistedTasks)._[name]; var elapsed = now - task.createdAt; var remaining = Math.max(task.remainingMs - elapsed, 0); result[name] = { createdAt: new Date(task.createdAt), remainingMs: remaining, readable: this.toReadable(remaining), meta: task.meta || {} }; } return result; } }, { key: "startTick", value: function startTick(interval) { var _this = this; var ms = typeof interval === "number" ? interval : this.toMilliseconds(interval); var label = typeof interval === "string" ? interval : "".concat(ms, "ms"); if (_assertClassBrand(TimeTide, this, _tickers)._[label]) { return; } _assertClassBrand(TimeTide, this, _tickers)._[label] = setInterval(function () { _this.events.emit("tick:".concat(label)); _this.events.emit("tick", label); }, ms); } }, { key: "stopTick", value: function stopTick(interval) { var label = typeof interval === "string" ? interval : "".concat(interval, "ms"); if (_assertClassBrand(TimeTide, this, _tickers)._[label]) { clearInterval(_assertClassBrand(TimeTide, this, _tickers)._[label]); delete _assertClassBrand(TimeTide, this, _tickers)._[label]; } } }, { key: "repeat", value: function repeat(interval, cb) { _assertClassBrand(TimeTide, this, _ensureTimersInitialized).call(this); var ms = typeof interval === "number" ? interval : this.toMilliseconds(interval); var id = _assertClassBrand(TimeTide, this, _generateID).call(this); var intv = setInterval(cb, ms); _assertClassBrand(TimeTide, this, _tasks)._[id] = intv; return id; } }, { key: "countdown", value: function countdown(dur, cb) { return this.schedule(dur, cb); } }, { key: "onEvery", value: function onEvery(interval, cb) { return this.repeat(interval, cb); } }, { key: "expired", value: function expired(date) { return new Date(date).getTime() < Date.now(); } }, { key: "getTimeUntil", value: function getTimeUntil(date) { return Math.max(new Date(date).getTime() - Date.now(), 0); } }, { key: "addTime", value: function addTime(date, dur) { return new Date(new Date(date).getTime() + this.toMilliseconds(dur)); } }, { key: "subtractTime", value: function subtractTime(date, dur) { return new Date(new Date(date).getTime() - this.toMilliseconds(dur)); } }]); }(); _TimeTide = TimeTide; function _generateID() { var _this$idCounter; return "tidetask_".concat(_idCounter._ = _assertClassBrand(_TimeTide, this, (_this$idCounter = _assertClassBrand(_TimeTide, this, _idCounter)._, ++_this$idCounter))); } function _emitUpdate() { this.events.emit("update", this.getTimers()); } function _saveTimers() { if (_assertClassBrand(_TimeTide, this, _config)._.disablePersistence) { return; } fs.writeFileSync(_assertClassBrand(_TimeTide, this, _config)._.persistPath, JSON.stringify(_assertClassBrand(_TimeTide, this, _persistedTasks)._, null, 5)); _assertClassBrand(_TimeTide, this, _emitUpdate).call(this); } function _ensureTimersInitialized() { if (!_assertClassBrand(_TimeTide, this, _timersInitialized)._ && !_assertClassBrand(_TimeTide, this, _config)._.disablePersistence) { _timersInitialized._ = _assertClassBrand(_TimeTide, this, true); this.loadTimers(); } } function _scheduleInternal(name, ms, cb) { var _this2 = this; var id = _assertClassBrand(_TimeTide, this, _generateID).call(this); var timeout = setTimeout(function () { cb(); if (name) { delete _assertClassBrand(_TimeTide, _this2, _persistedTasks)._[name]; _assertClassBrand(_TimeTide, _this2, _saveTimers).call(_this2); if (_assertClassBrand(_TimeTide, _this2, _chainedTasks)._[name]) { _assertClassBrand(_TimeTide, _this2, _chainedTasks)._[name](); delete _assertClassBrand(_TimeTide, _this2, _chainedTasks)._[name]; } } delete _assertClassBrand(_TimeTide, _this2, _tasks)._[name || id]; }, ms); _assertClassBrand(_TimeTide, this, _tasks)._[name || id] = timeout; return id; } var _config = { _: { persistPath: path.join(__dirname, ".timers.json"), disablePersistence: false } }; _defineProperty(TimeTide, "events", new EventEmitter()); var _timersInitialized = { _: false }; var _namedCallbacks = { _: {} }; var _persistedTasks = { _: {} }; var _chainedTasks = { _: {} }; var _idCounter = { _: 0 }; var _tickers = { _: {} }; var _tasks = { _: {} }; ; TimeTide.initializeTimers = function () { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; TimeTide.configure(options); TimeTide.loadTimers(); }; Object.assign(TimeTide, TimeUtilitiesManager); module.exports = TimeTide;