From d544bf466500727233bea8192a24d9cfc6fbed1f Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Thu, 22 Feb 2024 00:16:36 -0500 Subject: [PATCH] seperated service worker zyklus and cyclist zyklus because of import constraints on service workers --- packages/core/clockworker.js | 2 +- packages/core/cyclist.mjs | 2 +- packages/core/neozyklus.js | 46 ++++++++++++++++++ packages/core/zyklus.js | 91 ++++++++++++++++++------------------ 4 files changed, 93 insertions(+), 48 deletions(-) create mode 100644 packages/core/neozyklus.js diff --git a/packages/core/clockworker.js b/packages/core/clockworker.js index 7ca04528..db9ef123 100644 --- a/packages/core/clockworker.js +++ b/packages/core/clockworker.js @@ -1,5 +1,5 @@ // eslint-disable-next-line no-undef -importScripts('./zyklus.js'); +importScripts('./neozyklus.js'); function getTime() { const precision = 10 ** 4; diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index f024c464..a8ccd30f 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -4,7 +4,7 @@ Copyright (C) 2022 Strudel contributors - see . */ -import * as createClock from './zyklus.js'; +import createClock from './zyklus'; import { logger } from './logger.mjs'; export class Cyclist { diff --git a/packages/core/neozyklus.js b/packages/core/neozyklus.js new file mode 100644 index 00000000..9ec4e775 --- /dev/null +++ b/packages/core/neozyklus.js @@ -0,0 +1,46 @@ +// 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 }; +}; diff --git a/packages/core/zyklus.js b/packages/core/zyklus.js index 7a9c7fa5..3d25b054 100644 --- a/packages/core/zyklus.js +++ b/packages/core/zyklus.js @@ -1,50 +1,49 @@ // will move to https://github.com/felixroos/zyklus // TODO: started flag -//TODO: fix tests not understanding "self" -if (typeof self !== 'undefined') { - self.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 }; + +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); + 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 }; } +export default createClock;