Adjust to new timer channel format.

This commit is contained in:
2025-09-22 23:26:56 +10:00
parent b21a5969e3
commit 0de496bf3c

View File

@@ -31,13 +31,15 @@ class Timer {
var minutes = Math.floor(dur_seconds / 60); var minutes = Math.floor(dur_seconds / 60);
var hours = Math.floor(minutes / 60); var hours = Math.floor(minutes / 60);
minutes = minutes - 60 * hours; minutes = minutes - 60 * hours;
// Change rendering mode based on hours or minutes left
timestr = String(hours).padStart(2, '0') + ':' + String(minutes).padStart(2, '0'); if (hours > 0) {
timestr = String(hours).padStart(2, '0') + ':' + String(minutes).padStart(2, '0');
} else {
timestr = String(minutes).padStart(2, '0') + ':' + String(seconds).padStart(2, '0');
}
} }
if (this.running) { this.renderer.update_time(timestr);
this.renderer.update_time(timestr);
}
} }
tick() { tick() {
@@ -47,7 +49,9 @@ class Timer {
return return
} }
this.render_time(); this.render_time();
setTimeout(this.tick.bind(this), 1000); if (this.running) {
setTimeout(this.tick.bind(this), 1000);
}
} }
} }
@@ -74,23 +78,13 @@ class TimerRenderer {
this.time_element.textContent = timestr; this.time_element.textContent = timestr;
} }
set_break_color () {
this.background.style.setProperty('--stage-color', 'var(--break-color)');
this.time_element.style.color = "var(--break-color)";
}
set_focus_color () {
this.background.style.setProperty('--stage-color', 'var(--focus-color)');
this.time_element.style.color = "var(--focus-color)";
}
} }
function communicate(websocket) { function communicate(websocket) {
console.log("Communicating"); console.log("Communicating");
const params = new URLSearchParams(window.location.search); const params = new URLSearchParams(window.location.search);
websocket.send(JSON.stringify({type: "init", channel: "SubTimer", channelid: params.get('channelid')})); websocket.send(JSON.stringify({type: "init", channel: "SubTimer", community: params.get('community')}));
var renderer = new TimerRenderer(); var renderer = new TimerRenderer();
let timer; let timer;
@@ -111,11 +105,20 @@ function communicate(websocket) {
timer = new Timer( timer = new Timer(
renderer, renderer,
new Date(args.end_at), new Date(args.end_at),
true args.running
) )
timer.tick(); timer.tick();
timer.running = args.running;
break; break;
case "noTimer":
if (timer != null) {
timer.renderer.update_time("--:--")
timer.destroying = true;
}
case "endTimer":
if (timer != null) {
timer.renderer.update_time("00:00")
timer.destroying = true;
}
default: default:
throw new Error(`Unsupported method requested: ${event.method}.`) throw new Error(`Unsupported method requested: ${event.method}.`)
} }