Fixing seqbeat

This commit is contained in:
2023-08-13 19:36:15 +02:00
parent b37aa57f6f
commit 3effb4f456
3 changed files with 1021 additions and 973 deletions

View File

@ -62,6 +62,8 @@ To build a standalone browser application using [Tauri](https://tauri.app/), you
- [ ] Give more information the local context of execution of every script
- print/display the internal iterator used in each script
- animate code to show which line is currently being executed
- [ ] More rhythmic generators
- [ ] Rendering static files (MIDI, MOD, etc...)
## Scheduler

1916
src/API.ts

File diff suppressed because it is too large Load Diff

View File

@ -1,18 +1,15 @@
import type { Editor } from './main';
import type { File } from './AppSettings';
import type { Editor } from "./main";
import type { File } from "./AppSettings";
function codeInterceptor(code: string): string {
return code
.replace(/->/g, "&&")
.replace(/t\[(\d+),(\d+)\]/g, 'mod($1,$2)')
.replace(/b\[(\d+),(\d+)\]/g, '[$1,$2].includes(beat)')
.replace(/eb\[(\d+),(\d+)\]/g, '[$1,$2].includes(ebeat)')
.replace(/m\[(\d+),(\d+)\]/g, '[$1,$2].includes(bar)')
}
const delay = (ms: number) =>
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Operation took too long")), ms)
);
const delay = (ms: number) => new Promise((_, reject) => setTimeout(() => reject(new Error('Operation took too long')), ms));
const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> => {
const tryCatchWrapper = (
application: Editor,
code: string
): Promise<boolean> => {
/**
* This function wraps a string of code in a try/catch block and returns a promise
* that resolves to true if the code is valid and false if the code is invalid after
@ -24,14 +21,16 @@ const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> =>
*/
return new Promise((resolve, _) => {
try {
Function(`with (this) {try{${codeInterceptor(code)}} catch (e) {console.log(e)}};`).call(application.api);
Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(
application.api
);
resolve(true);
} catch (error) {
console.log(error);
resolve(false);
}
});
}
};
export const tryEvaluate = async (
/**
@ -47,13 +46,16 @@ export const tryEvaluate = async (
application: Editor,
code: File,
timeout = 5000
): Promise<void> => {
): Promise<void> => {
try {
code.evaluations!++;
const isCodeValid = await Promise.race([tryCatchWrapper(
application,
`let i = ${code.evaluations};` + codeInterceptor(code.candidate as string),
), delay(timeout)]);
const isCodeValid = await Promise.race([
tryCatchWrapper(
application,
(`let i = ${code.evaluations};` + code.candidate) as string
),
delay(timeout),
]);
if (isCodeValid) {
code.committed = code.candidate;
@ -63,9 +65,13 @@ export const tryEvaluate = async (
} catch (error) {
console.log(error);
}
}
};
export const evaluate = async (application: Editor, code: File, timeout = 1000): Promise<void> => {
export const evaluate = async (
application: Editor,
code: File,
timeout = 1000
): Promise<void> => {
/**
* This function evaluates a string of code in the context of user API.
*
@ -75,10 +81,12 @@ export const evaluate = async (application: Editor, code: File, timeout = 1000):
* @returns A promise that resolves to void
*/
try {
await Promise.race([tryCatchWrapper(application, codeInterceptor(code.committed as string)), delay(timeout)]);
if (code.evaluations)
code.evaluations++;
await Promise.race([
tryCatchWrapper(application, code.committed as string),
delay(timeout),
]);
if (code.evaluations) code.evaluations++;
} catch (error) {
console.log(error);
}
}
};