repl reset default dict

+ add mechanism to get a module by name from dynamic imports
This commit is contained in:
Felix Roos 2024-02-29 04:54:03 +01:00
parent d4eebf1a77
commit 305715277c
3 changed files with 13 additions and 4 deletions

View file

@ -22,6 +22,7 @@ export const evalScope = async (...args) => {
globalThis[name] = value; globalThis[name] = value;
}); });
}); });
return modules;
}; };
function safeEval(str, options = {}) { function safeEval(str, options = {}) {

View file

@ -5,3 +5,5 @@ export * from './tonal.mjs';
export * from './voicings.mjs'; export * from './voicings.mjs';
import './ireal.mjs'; import './ireal.mjs';
export const packageName = '@strudel/tonal';

View file

@ -48,6 +48,14 @@ if (typeof window !== 'undefined') {
isIframe = window.location !== window.parent.location; isIframe = window.location !== window.parent.location;
} }
async function getModule(name) {
if (!modulesLoading) {
return;
}
const modules = await modulesLoading;
return modules.find((m) => m.packageName === name);
}
export function Repl({ embedded = false }) { export function Repl({ embedded = false }) {
const isEmbedded = embedded || isIframe; const isEmbedded = embedded || isIframe;
const { panelPosition, isZen } = useSettings(); const { panelPosition, isZen } = useSettings();
@ -163,6 +171,7 @@ export function Repl({ embedded = false }) {
}; };
const resetEditor = async () => { const resetEditor = async () => {
(await getModule('@strudel/tonal'))?.resetVoicings();
resetGlobalEffects(); resetGlobalEffects();
clearCanvas(); clearCanvas();
resetLoadedSounds(); resetLoadedSounds();
@ -188,10 +197,7 @@ export function Repl({ embedded = false }) {
logger(`[repl] ✨ loading random tune "${patternData.id}"`); logger(`[repl] ✨ loading random tune "${patternData.id}"`);
setActivePattern(patternData.id); setActivePattern(patternData.id);
setViewingPatternData(patternData); setViewingPatternData(patternData);
clearCanvas(); await resetEditor();
resetLoadedSounds();
resetGlobalEffects();
await prebake(); // declare default samples
editorRef.current.setCode(code); editorRef.current.setCode(code);
editorRef.current.repl.evaluate(code); editorRef.current.repl.evaluate(code);
}; };