diff --git a/packages/core/clockworker.js b/packages/core/clockworker.js index e6165bd5..f524e439 100644 --- a/packages/core/clockworker.js +++ b/packages/core/clockworker.js @@ -1,5 +1,4 @@ // eslint-disable-next-line no-undef -importScripts('./neozyklus.js'); // TODO: swap below line with above one when firefox supports esm imports in service workers // see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker?retiredLocale=de#browser_compatibility // import createClock from './zyklus.mjs'; @@ -58,7 +57,7 @@ const sendTick = (phase, duration, tick, time) => { }; //create clock method from zyklus -const clock = this.createClock(getTime, sendTick, duration); +const clock = createClock(getTime, sendTick, duration); let started = false; const startClock = (id) => { @@ -117,7 +116,7 @@ const processMessage = (message) => { } }; -this.onconnect = function (e) { +self.onconnect = function (e) { // the incoming port const port = e.ports[0]; @@ -126,3 +125,50 @@ this.onconnect = function (e) { }); port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter. }; + +// used to consistently schedule events, for use in a service worker - see +function createClock( + getTime, + callback, // called slightly before each cycle + duration = 0.05, // duration of each cycle + interval = 0.1, // interval between callbacks + overlap = 0.1, // overlap between callbacks +) { + let tick = 0; // counts callbacks + let phase = 0; // next callback time + let precision = 10 ** 4; // used to round phase + let minLatency = 0.01; + const setDuration = (setter) => (duration = setter(duration)); + overlap = overlap || interval / 2; + const onTick = () => { + const t = getTime(); + const lookahead = t + interval + overlap; // the time window for this tick + if (phase === 0) { + phase = t + minLatency; + } + // callback as long as we're inside the lookahead + while (phase < lookahead) { + phase = Math.round(phase * precision) / precision; + phase >= t && callback(phase, duration, tick, t); + phase < t && console.log('TOO LATE', phase); // what if latency is added from outside? + phase += duration; // increment phase by duration + tick++; + } + }; + let intervalID; + const start = () => { + clear(); // just in case start was called more than once + onTick(); + intervalID = setInterval(onTick, interval * 1000); + }; + const clear = () => intervalID !== undefined && clearInterval(intervalID); + const pause = () => clear(); + const stop = () => { + tick = 0; + phase = 0; + clear(); + }; + const getPhase = () => phase; + // setCallback + return { setDuration, start, stop, pause, duration, interval, getPhase, minLatency }; +} diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 91e0828e..3ca40178 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -54,7 +54,6 @@ export class NeoCyclist { const time = this.getTime(); let { - cycle, phase, num_cycles_at_cps_change, cps, diff --git a/packages/core/neozyklus.js b/packages/core/neozyklus.js deleted file mode 100644 index 9ec4e775..00000000 --- a/packages/core/neozyklus.js +++ /dev/null @@ -1,46 +0,0 @@ -// used to consistently schedule events, for use in a service worker - see -this.createClock = ( - getTime, - callback, // called slightly before each cycle - duration = 0.05, // duration of each cycle - interval = 0.1, // interval between callbacks - overlap = 0.1, // overlap between callbacks -) => { - let tick = 0; // counts callbacks - let phase = 0; // next callback time - let precision = 10 ** 4; // used to round phase - let minLatency = 0.01; - const setDuration = (setter) => (duration = setter(duration)); - overlap = overlap || interval / 2; - const onTick = () => { - const t = getTime(); - const lookahead = t + interval + overlap; // the time window for this tick - if (phase === 0) { - phase = t + minLatency; - } - // callback as long as we're inside the lookahead - while (phase < lookahead) { - phase = Math.round(phase * precision) / precision; - phase >= t && callback(phase, duration, tick, t); - phase < t && console.log('TOO LATE', phase); // what if latency is added from outside? - phase += duration; // increment phase by duration - tick++; - } - }; - let intervalID; - const start = () => { - clear(); // just in case start was called more than once - onTick(); - intervalID = setInterval(onTick, interval * 1000); - }; - const clear = () => intervalID !== undefined && clearInterval(intervalID); - const pause = () => clear(); - const stop = () => { - tick = 0; - phase = 0; - clear(); - }; - const getPhase = () => phase; - // setCallback - return { setDuration, start, stop, pause, duration, interval, getPhase, minLatency }; -};