add beforeStart callback

This commit is contained in:
Felix Roos 2024-06-02 02:34:33 +02:00
parent 0e6a17fc77
commit 94e411aa7a
2 changed files with 21 additions and 7 deletions

View File

@ -8,8 +8,19 @@ import createClock from './zyklus.mjs';
import { logger } from './logger.mjs'; import { logger } from './logger.mjs';
export class Cyclist { export class Cyclist {
constructor({ interval, onTrigger, onToggle, onError, getTime, latency = 0.1, setInterval, clearInterval }) { constructor({
interval,
onTrigger,
onToggle,
onError,
getTime,
latency = 0.1,
setInterval,
clearInterval,
beforeStart,
}) {
this.started = false; this.started = false;
this.beforeStart = beforeStart;
this.cps = 0.5; this.cps = 0.5;
this.num_ticks_since_cps_change = 0; this.num_ticks_since_cps_change = 0;
this.lastTick = 0; // absolute time when last tick (clock callback) happened this.lastTick = 0; // absolute time when last tick (clock callback) happened
@ -82,7 +93,8 @@ export class Cyclist {
this.started = v; this.started = v;
this.onToggle?.(v); this.onToggle?.(v);
} }
start() { async start() {
await this.beforeStart?.();
this.num_ticks_since_cps_change = 0; this.num_ticks_since_cps_change = 0;
this.num_cycles_at_cps_change = 0; this.num_cycles_at_cps_change = 0;
if (!this.pattern) { if (!this.pattern) {
@ -103,10 +115,10 @@ export class Cyclist {
this.lastEnd = 0; this.lastEnd = 0;
this.setStarted(false); this.setStarted(false);
} }
setPattern(pat, autostart = false) { async setPattern(pat, autostart = false) {
this.pattern = pat; this.pattern = pat;
if (autostart && !this.started) { if (autostart && !this.started) {
this.start(); await this.start();
} }
} }
setCps(cps = 0.5) { setCps(cps = 0.5) {

View File

@ -10,6 +10,7 @@ export function repl({
defaultOutput, defaultOutput,
onEvalError, onEvalError,
beforeEval, beforeEval,
beforeStart,
afterEval, afterEval,
getTime, getTime,
transpiler, transpiler,
@ -53,6 +54,7 @@ export function repl({
}, },
setInterval, setInterval,
clearInterval, clearInterval,
beforeStart,
}; };
// NeoCyclist uses a shared worker to communicate between instances, which is not supported on mobile chrome // NeoCyclist uses a shared worker to communicate between instances, which is not supported on mobile chrome
@ -69,9 +71,9 @@ export function repl({
return silence; return silence;
}; };
const setPattern = (pattern, autostart = true) => { const setPattern = async (pattern, autostart = true) => {
pattern = editPattern?.(pattern) || pattern; pattern = editPattern?.(pattern) || pattern;
scheduler.setPattern(pattern, autostart); await scheduler.setPattern(pattern, autostart);
}; };
setTime(() => scheduler.now()); // TODO: refactor? setTime(() => scheduler.now()); // TODO: refactor?
@ -159,7 +161,7 @@ export function repl({
throw new Error(message + (typeof evaluated === 'function' ? ', did you forget to call a function?' : '.')); throw new Error(message + (typeof evaluated === 'function' ? ', did you forget to call a function?' : '.'));
} }
logger(`[eval] code updated`); logger(`[eval] code updated`);
setPattern(pattern, autostart); await setPattern(pattern, autostart);
updateState({ updateState({
miniLocations: meta?.miniLocations || [], miniLocations: meta?.miniLocations || [],
widgets: meta?.widgets || [], widgets: meta?.widgets || [],