stop clock if all stopped

This commit is contained in:
Jade (Rose) Rowland 2024-03-10 14:35:43 -04:00
parent 30a5176090
commit 112ca1875a
2 changed files with 17 additions and 9 deletions

View File

@ -10,10 +10,11 @@ function getTime() {
return Math.round(seconds * precision) / precision; return Math.round(seconds * precision) / precision;
} }
let numPorts = 0;
let num_cycles_at_cps_change = 0; let num_cycles_at_cps_change = 0;
let num_ticks_since_cps_change = 0; let num_ticks_since_cps_change = 0;
let cps = 0.5; let cps = 0.5;
// {id: {started: boolean}}
const clients = new Map();
const duration = 0.1; const duration = 0.1;
const channel = new BroadcastChannel('strudeltick'); const channel = new BroadcastChannel('strudeltick');
@ -37,18 +38,23 @@ const sendTick = (phase, duration, tick, time) => {
const clock = this.createClock(getTime, sendTick, duration); const clock = this.createClock(getTime, sendTick, duration);
let started = false; let started = false;
const startClock = () => { const startClock = (id) => {
clients.set(id, { started: true });
if (started) { if (started) {
return; return;
} }
clock.start(); clock.start();
started = true; started = true;
}; };
const stopClock = async () => { const stopClock = async (id) => {
//dont stop the clock if mutliple instances are using it... clients.set(id, { started: false });
if (!started || numPorts !== 1) {
const otherClientStarted = Array.from(clients.values()).some((c) => c.started);
//dont stop the clock if other instances are running...
if (!started || otherClientStarted) {
return; return;
} }
clock.stop(); clock.stop();
setCycle(0); setCycle(0);
started = false; started = false;
@ -77,9 +83,9 @@ const processMessage = (message) => {
} }
case 'toggle': { case 'toggle': {
if (payload.started) { if (payload.started) {
startClock(); startClock(message.id);
} else { } else {
stopClock(); stopClock(message.id);
} }
break; break;
} }
@ -89,8 +95,9 @@ const processMessage = (message) => {
this.onconnect = function (e) { this.onconnect = function (e) {
// the incoming port // the incoming port
const port = e.ports[0]; const port = e.ports[0];
numPorts = numPorts + 1;
port.addEventListener('message', function (e) { port.addEventListener('message', function (e) {
console.log(e.data);
processMessage(e.data); processMessage(e.data);
}); });
port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter. port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.

View File

@ -17,6 +17,7 @@ export class NeoCyclist {
this.onToggle = onToggle; this.onToggle = onToggle;
this.latency = 0.1; // fixed trigger time offset this.latency = 0.1; // fixed trigger time offset
this.cycle = 0; this.cycle = 0;
this.id = Math.round(Date.now() * Math.random());
this.worker = new SharedWorker(new URL('./clockworker.js', import.meta.url)); this.worker = new SharedWorker(new URL('./clockworker.js', import.meta.url));
this.worker.port.start(); this.worker.port.start();
@ -107,7 +108,7 @@ export class NeoCyclist {
}; };
} }
sendMessage(type, payload) { sendMessage(type, payload) {
this.worker.port.postMessage({ type, payload }); this.worker.port.postMessage({ type, payload, id: this.id });
} }
now() { now() {