inject scheduler into Pattern methods right before eval
This commit is contained in:
parent
0a82471112
commit
aa1b9d11dc
1 changed files with 50 additions and 45 deletions
|
|
@ -61,45 +61,7 @@ export function repl({
|
||||||
scheduler.setPattern(pattern, autostart);
|
scheduler.setPattern(pattern, autostart);
|
||||||
};
|
};
|
||||||
setTime(() => scheduler.now()); // TODO: refactor?
|
setTime(() => scheduler.now()); // TODO: refactor?
|
||||||
const evaluate = async (code, autostart = true, shouldHush = true) => {
|
|
||||||
if (!code) {
|
|
||||||
throw new Error('no code to evaluate');
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
updateState({ code, pending: true });
|
|
||||||
await beforeEval?.({ code });
|
|
||||||
shouldHush && hush();
|
|
||||||
let { pattern, meta } = await _evaluate(code, transpiler);
|
|
||||||
if (Object.keys(pPatterns).length) {
|
|
||||||
pattern = stack(...Object.values(pPatterns));
|
|
||||||
}
|
|
||||||
if (allTransform) {
|
|
||||||
pattern = allTransform(pattern);
|
|
||||||
}
|
|
||||||
if (!isPattern(pattern)) {
|
|
||||||
const message = `got "${typeof evaluated}" instead of pattern`;
|
|
||||||
throw new Error(message + (typeof evaluated === 'function' ? ', did you forget to call a function?' : '.'));
|
|
||||||
}
|
|
||||||
logger(`[eval] code updated`);
|
|
||||||
setPattern(pattern, autostart);
|
|
||||||
updateState({
|
|
||||||
miniLocations: meta?.miniLocations || [],
|
|
||||||
widgets: meta?.widgets || [],
|
|
||||||
activeCode: code,
|
|
||||||
pattern,
|
|
||||||
evalError: undefined,
|
|
||||||
schedulerError: undefined,
|
|
||||||
pending: false,
|
|
||||||
});
|
|
||||||
afterEval?.({ code, pattern, meta });
|
|
||||||
return pattern;
|
|
||||||
} catch (err) {
|
|
||||||
// console.warn(`[repl] eval error: ${err.message}`);
|
|
||||||
logger(`[eval] error: ${err.message}`, 'error');
|
|
||||||
updateState({ evalError: err, pending: false });
|
|
||||||
onEvalError?.(err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const stop = () => scheduler.stop();
|
const stop = () => scheduler.stop();
|
||||||
const start = () => scheduler.start();
|
const start = () => scheduler.start();
|
||||||
const pause = () => scheduler.pause();
|
const pause = () => scheduler.pause();
|
||||||
|
|
@ -107,6 +69,7 @@ export function repl({
|
||||||
const setCps = (cps) => scheduler.setCps(cps);
|
const setCps = (cps) => scheduler.setCps(cps);
|
||||||
const setCpm = (cpm) => scheduler.setCps(cpm / 60);
|
const setCpm = (cpm) => scheduler.setCps(cpm / 60);
|
||||||
|
|
||||||
|
const injectPatternMethods = () => {
|
||||||
Pattern.prototype.p = function (id) {
|
Pattern.prototype.p = function (id) {
|
||||||
pPatterns[id] = this;
|
pPatterns[id] = this;
|
||||||
return this;
|
return this;
|
||||||
|
|
@ -145,6 +108,48 @@ export function repl({
|
||||||
setCpm,
|
setCpm,
|
||||||
setcpm: setCpm,
|
setcpm: setCpm,
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const evaluate = async (code, autostart = true, shouldHush = true) => {
|
||||||
|
if (!code) {
|
||||||
|
throw new Error('no code to evaluate');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
updateState({ code, pending: true });
|
||||||
|
injectPatternMethods();
|
||||||
|
await beforeEval?.({ code });
|
||||||
|
shouldHush && hush();
|
||||||
|
let { pattern, meta } = await _evaluate(code, transpiler);
|
||||||
|
if (Object.keys(pPatterns).length) {
|
||||||
|
pattern = stack(...Object.values(pPatterns));
|
||||||
|
}
|
||||||
|
if (allTransform) {
|
||||||
|
pattern = allTransform(pattern);
|
||||||
|
}
|
||||||
|
if (!isPattern(pattern)) {
|
||||||
|
const message = `got "${typeof evaluated}" instead of pattern`;
|
||||||
|
throw new Error(message + (typeof evaluated === 'function' ? ', did you forget to call a function?' : '.'));
|
||||||
|
}
|
||||||
|
logger(`[eval] code updated`);
|
||||||
|
setPattern(pattern, autostart);
|
||||||
|
updateState({
|
||||||
|
miniLocations: meta?.miniLocations || [],
|
||||||
|
widgets: meta?.widgets || [],
|
||||||
|
activeCode: code,
|
||||||
|
pattern,
|
||||||
|
evalError: undefined,
|
||||||
|
schedulerError: undefined,
|
||||||
|
pending: false,
|
||||||
|
});
|
||||||
|
afterEval?.({ code, pattern, meta });
|
||||||
|
return pattern;
|
||||||
|
} catch (err) {
|
||||||
|
// console.warn(`[repl] eval error: ${err.message}`);
|
||||||
|
logger(`[eval] error: ${err.message}`, 'error');
|
||||||
|
updateState({ evalError: err, pending: false });
|
||||||
|
onEvalError?.(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
const setCode = (code) => updateState({ code });
|
const setCode = (code) => updateState({ code });
|
||||||
return { scheduler, evaluate, start, stop, pause, setCps, setPattern, setCode, toggle, state };
|
return { scheduler, evaluate, start, stop, pause, setCps, setPattern, setCode, toggle, state };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue