patterns can now be async

This commit is contained in:
Felix Roos
2022-02-27 20:08:56 +01:00
parent 634d4f1099
commit 4f2f00ee62
5 changed files with 45 additions and 11 deletions
+4 -4
View File
@@ -51,11 +51,11 @@ function App() {
// set active pattern on ctrl+enter
useLayoutEffect(() => {
// TODO: make sure this is only fired when editor has focus
const handleKeyPress = (e: any) => {
const handleKeyPress = async (e: any) => {
if (e.ctrlKey || e.altKey) {
switch (e.code) {
case 'Enter':
activateCode();
await activateCode();
!cycle.started && cycle.start();
break;
case 'Period':
@@ -88,11 +88,11 @@ function App() {
</div>
<div className="flex space-x-4">
<button
onClick={() => {
onClick={async () => {
const _code = getRandomTune();
console.log('tune', _code); // uncomment this to debug when random code fails
setCode(_code);
const parsed = evaluate(_code);
const parsed = await evaluate(_code);
// Tone.Transport.cancel(Tone.Transport.seconds);
setPattern(parsed.pattern);
}}
+2 -2
View File
@@ -29,10 +29,10 @@ hackLiteral(String, ['pure', 'p'], bootstrapped.pure); // comment out this line
// this will add everything to global scope, which is accessed by eval
Object.assign(globalThis, bootstrapped, Tone, toneHelpers);
export const evaluate: any = (code: string) => {
export const evaluate: any = async (code: string) => {
const shapeshifted = shapeshifter(code); // transform syntactically correct js code to semantically usable code
// console.log('shapeshifted', shapeshifted);
let evaluated = eval(shapeshifted);
let evaluated = await eval(shapeshifted);
if (typeof evaluated === 'function') {
evaluated = evaluated();
}
+4 -4
View File
@@ -21,15 +21,15 @@ function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw }: any)
const [pattern, setPattern] = useState<Pattern>();
const dirty = code !== activeCode || error;
const generateHash = () => encodeURIComponent(btoa(code));
const activateCode = (_code = code) => {
!cycle.started && cycle.start();
broadcast({ type: 'start', from: id });
const activateCode = async (_code = code) => {
if (activeCode && !dirty) {
setError(undefined);
return;
}
try {
const parsed = evaluate(_code);
const parsed = await evaluate(_code);
!cycle.started && cycle.start();
broadcast({ type: 'start', from: id });
setPattern(() => parsed.pattern);
if (autolink) {
window.location.hash = '#' + encodeURIComponent(btoa(code));