// presenter-clock.mjs — the presenter window's timer and clock (spec // §5, §5.5): the timer in numbers only, from the talk's first press, // and the clock in the room's own time. No words. // // The timer's start is its element's data-timer-since (Unix seconds), // which the server draws, a press in this window sets at once (press), // and the server's word sets from then on, whichever window pressed // (heard: the talk's timerAt in ms, or null after a reset). const MOVING = new Set(['next', 'back', 'first', 'last', 'goto']) /** The timer and the clock, ticking. timer and clock may be absent. */ export function mountClock (timer, clock) { const since = () => timer && timer.getAttribute('data-timer-since') const two = n => (n < 10 ? '0' : '') + n function timerNow () { const at = since() if (!at) return const s = Math.max(0, Math.floor(Date.now() / 1000 - Number(at))) const h = Math.floor(s / 3600) const m = Math.floor(s / 60) % 60 timer.textContent = h ? h + ':' + two(m) + ':' + two(s % 60) : m + ':' + two(s % 60) } function clockNow () { if (clock) clock.textContent = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) } function reset () { if (!since()) return timer.removeAttribute('data-timer-since') timer.textContent = '0:00' } timerNow() clockNow() setInterval(timerNow, 1000) setInterval(clockNow, 15000) return { /** A press made here: a move starts a timer that is not running. */ press (move) { if (timer && MOVING.has(move.op) && !since()) timer.setAttribute('data-timer-since', String(Math.floor(Date.now() / 1000))) }, /** The server's timerAt: Unix ms, or null for a timer reset. */ heard (ms) { if (!timer) return if (ms === null) { reset(); return } const at = String(Math.floor(ms / 1000)) if (since() !== at) timer.setAttribute('data-timer-since', at) }, /** Back to 0:00, waiting for the next press. */ reset } }