UNPKG

wise-eyes-core

Version:

Web server to monitor the status of owlcms

77 lines (76 loc) 2.64 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); var Clock = /** @class */ (function () { function Clock(_a) { var _b = _a === void 0 ? {} : _a, _c = _b.isStopped, isStopped = _c === void 0 ? true : _c, _d = _b.milliseconds, milliseconds = _d === void 0 ? 0 : _d; this.isStopped = true; this.milliseconds = 0; this.update({ isStopped: isStopped, milliseconds: milliseconds, }); } Clock.prototype.getCurrentMilliseconds = function () { if (this.isStopped) { return this.milliseconds; } return Math.max(0, this.milliseconds - (Date.now() - this.setAt)); }; Clock.prototype.getDisplayValue = function (milliseconds) { if (this.isIndefinite()) { return ''; } var seconds = Math.round(milliseconds / 1000); var hours = Math.floor(seconds / 60 / 60); seconds -= hours * 60 * 60; var minutes = Math.floor(seconds / 60); seconds -= minutes * 60; var parts = []; if (hours > 0) { parts = [ hours, ':', minutes < 10 ? '0' : '', ]; } parts = __spreadArray(__spreadArray([], parts, true), [ minutes, ':', seconds < 10 ? '0' : '', seconds, ], false); return parts.join(''); }; Clock.prototype.getState = function () { var milliseconds = this.getCurrentMilliseconds(); return { displayValue: this.getDisplayValue(milliseconds), indefinite: this.isIndefinite(), isStopped: this.isStopped, milliseconds: milliseconds, }; }; Clock.prototype.isIndefinite = function () { return this.milliseconds === Number.POSITIVE_INFINITY; }; Clock.prototype.update = function (_a) { var isStopped = _a.isStopped, milliseconds = _a.milliseconds; this.isStopped = isStopped; if (milliseconds == null) { return; } this.milliseconds = Math.max(0, milliseconds); this.setAt = Date.now(); }; return Clock; }()); exports.default = Clock;