importScripts

This commit is contained in:
Jade (Rose) Rowland 2024-02-08 00:20:40 -05:00
parent 5a3272fe29
commit b37c560ea5
2 changed files with 90 additions and 39 deletions

View File

@ -1,4 +1,7 @@
function getTime(precision) { importScripts('./zyklus.js');
function getTime() {
const precision = 10 ** 4;
const seconds = performance.now() / 1000; const seconds = performance.now() / 1000;
return Math.round(seconds * precision) / precision; return Math.round(seconds * precision) / precision;
} }
@ -14,7 +17,7 @@ const sendMessage = (type, payload) => {
channel.postMessage({ type, payload }); channel.postMessage({ type, payload });
}; };
const sendTick = ({ phase, duration, time }) => { const sendTick = (phase, duration, tick, time) => {
sendMessage('tick', { sendMessage('tick', {
phase, phase,
duration, duration,
@ -26,7 +29,7 @@ const sendTick = ({ phase, duration, time }) => {
num_ticks_since_cps_change++; num_ticks_since_cps_change++;
}; };
const clock = createClock(sendTick, duration); const clock = this.createClock(getTime, sendTick, duration);
let started = false; let started = false;
const startClock = () => { const startClock = () => {
@ -88,41 +91,41 @@ this.onconnect = function (e) {
port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter. port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.
}; };
function createClock( // function createClock(
callback, // called slightly before each cycle // callback, // called slightly before each cycle
duration, // duration,
) { // ) {
const interval = 0.1; // const interval = 0.1;
const overlap = interval / 2; // const overlap = interval / 2;
const precision = 10 ** 4; // used to round phase // const precision = 10 ** 4; // used to round phase
const minLatency = 0.01; // const minLatency = 0.01;
let phase = 0; // next callback time // let phase = 0; // next callback time
const onTick = () => { // const onTick = () => {
const t = getTime(precision); // const t = getTime(precision);
const lookahead = t + interval + overlap; // the time window for this tick // const lookahead = t + interval + overlap; // the time window for this tick
if (phase === 0) { // if (phase === 0) {
phase = t + minLatency; // phase = t + minLatency;
} // }
// callback as long as we're inside the lookahead // // callback as long as we're inside the lookahead
while (phase < lookahead) { // while (phase < lookahead) {
phase = Math.round(phase * precision) / precision; // phase = Math.round(phase * precision) / precision;
phase >= t && callback({ phase, duration, time: t }); // phase >= t && callback({ phase, duration, time: t });
phase < t && console.log('TOO LATE', phase); // what if latency is added from outside? // phase < t && console.log('TOO LATE', phase); // what if latency is added from outside?
phase += duration; // increment phase by duration // phase += duration; // increment phase by duration
} // }
}; // };
let intervalID; // let intervalID;
const start = () => { // const start = () => {
clear(); // just in case start was called more than once // clear(); // just in case start was called more than once
onTick(); // onTick();
intervalID = setInterval(onTick, interval * 1000); // intervalID = setInterval(onTick, interval * 1000);
}; // };
const clear = () => intervalID !== undefined && clearInterval(intervalID); // const clear = () => intervalID !== undefined && clearInterval(intervalID);
const stop = () => { // const stop = () => {
phase = 0; // phase = 0;
clear(); // clear();
}; // };
return { start, stop }; // return { start, stop };
} // }

48
packages/core/zyklus.js Normal file
View File

@ -0,0 +1,48 @@
// will move to https://github.com/felixroos/zyklus
// TODO: started flag
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 };
};