UNPKG

wise-eyes-core

Version:

Web server to monitor the status of owlcms

67 lines (66 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Clock { isStopped = true; milliseconds = 0; setAt; constructor({ isStopped = true, milliseconds = 0, } = {}) { this.update({ isStopped, milliseconds, }); } getCurrentMilliseconds() { if (this.isStopped) { return this.milliseconds; } return Math.max(0, this.milliseconds - (Date.now() - this.setAt)); } getDisplayValue(milliseconds) { if (this.isIndefinite()) { return ''; } let seconds = Math.round(milliseconds / 1000); const hours = Math.floor(seconds / 60 / 60); seconds -= hours * 60 * 60; const minutes = Math.floor(seconds / 60); seconds -= minutes * 60; let parts = []; if (hours > 0) { parts = [ hours, ':', minutes < 10 ? '0' : '', ]; } parts = [ ...parts, minutes, ':', seconds < 10 ? '0' : '', seconds, ]; return parts.join(''); } getState() { const milliseconds = this.getCurrentMilliseconds(); return { displayValue: this.getDisplayValue(milliseconds), indefinite: this.isIndefinite(), isStopped: this.isStopped, milliseconds, }; } isIndefinite() { return this.milliseconds === Number.POSITIVE_INFINITY; } update({ isStopped, milliseconds, }) { this.isStopped = isStopped; if (milliseconds == null) { return; } this.milliseconds = Math.max(0, milliseconds); this.setAt = Date.now(); } } exports.default = Clock;