Introducing the magical I variable
This commit is contained in:
44
src/API.ts
44
src/API.ts
@ -5,10 +5,10 @@ import { MidiConnection } from "./IO/MidiConnection";
|
||||
// @ts-ignore
|
||||
import { webaudioOutput, samples } from '@strudel.cycles/webaudio';
|
||||
|
||||
// const sound = (value: any) => ({
|
||||
// value, context: {},
|
||||
// ensureObjectValue: () => {}
|
||||
// });
|
||||
const sound = (value: any) => ({
|
||||
value, context: {},
|
||||
ensureObjectValue: () => {}
|
||||
});
|
||||
|
||||
class DrunkWalk {
|
||||
public min: number;
|
||||
@ -66,7 +66,7 @@ export class UserAPI {
|
||||
load: samples
|
||||
|
||||
constructor (public app: Editor) {
|
||||
// this.load = samples("github:tidalcycles/Dirt-Samples/master");
|
||||
this.load = samples("github:tidalcycles/Dirt-Samples/master");
|
||||
}
|
||||
|
||||
// =============================================================
|
||||
@ -107,8 +107,10 @@ export class UserAPI {
|
||||
|
||||
script(...args: number[]): void {
|
||||
args.forEach(arg => {
|
||||
tryEvaluate(this.app, this.app.universes[
|
||||
this.app.selected_universe].locals[arg])
|
||||
tryEvaluate(
|
||||
this.app,
|
||||
this.app.universes[this.app.selected_universe].locals[arg],
|
||||
)
|
||||
})
|
||||
}
|
||||
s = this.script
|
||||
@ -314,19 +316,15 @@ export class UserAPI {
|
||||
// =============================================================
|
||||
|
||||
get i() { return this.app.universes[this.app.selected_universe].global.evaluations }
|
||||
get e1() { return this.app.universes[this.app.selected_universe].locals[0].evaluations }
|
||||
get e2() { return this.app.universes[this.app.selected_universe].locals[1].evaluations }
|
||||
get e3() { return this.app.universes[this.app.selected_universe].locals[2].evaluations }
|
||||
get e4() { return this.app.universes[this.app.selected_universe].locals[3].evaluations }
|
||||
get e5() { return this.app.universes[this.app.selected_universe].locals[4].evaluations }
|
||||
get e6() { return this.app.universes[this.app.selected_universe].locals[5].evaluations }
|
||||
get e7() { return this.app.universes[this.app.selected_universe].locals[6].evaluations }
|
||||
get e8() { return this.app.universes[this.app.selected_universe].locals[7].evaluations }
|
||||
get e9() { return this.app.universes[this.app.selected_universe].locals[8].evaluations }
|
||||
public evaluations_number(index: number): number {
|
||||
return this.app.universes[this.app.selected_universe].locals[index].evaluations
|
||||
}
|
||||
e = this.evaluations_number
|
||||
get e1() { return this.app.universes[this.app.selected_universe].locals[1].evaluations }
|
||||
get e2() { return this.app.universes[this.app.selected_universe].locals[2].evaluations }
|
||||
get e3() { return this.app.universes[this.app.selected_universe].locals[3].evaluations }
|
||||
get e4() { return this.app.universes[this.app.selected_universe].locals[4].evaluations }
|
||||
get e5() { return this.app.universes[this.app.selected_universe].locals[5].evaluations }
|
||||
get e6() { return this.app.universes[this.app.selected_universe].locals[6].evaluations }
|
||||
get e7() { return this.app.universes[this.app.selected_universe].locals[7].evaluations }
|
||||
get e8() { return this.app.universes[this.app.selected_universe].locals[8].evaluations }
|
||||
get e9() { return this.app.universes[this.app.selected_universe].locals[9].evaluations }
|
||||
|
||||
// =============================================================
|
||||
// Time markers
|
||||
@ -416,7 +414,7 @@ export class UserAPI {
|
||||
// Trivial functions
|
||||
// =============================================================
|
||||
|
||||
// sound = async (values: object) => {
|
||||
// webaudioOutput(sound(values), 0.00)
|
||||
// }
|
||||
sound = async (values: object) => {
|
||||
webaudioOutput(sound(values), 0.00)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import type { Editor } from './main';
|
||||
import type { File } from './AppSettings';
|
||||
|
||||
const delay = (ms: number) => new Promise((resolve, 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> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise((resolve, _) => {
|
||||
try {
|
||||
Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(application.api);
|
||||
resolve(true);
|
||||
@ -15,10 +15,17 @@ const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> =>
|
||||
});
|
||||
}
|
||||
|
||||
export const tryEvaluate = async (application: Editor, code: File, timeout = 5000): Promise<void> => {
|
||||
export const tryEvaluate = async (
|
||||
application: Editor,
|
||||
code: File,
|
||||
timeout = 5000
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const isCodeValid = await Promise.race([tryCatchWrapper(application, code.candidate), delay(timeout)]);
|
||||
code.evaluations++;
|
||||
const isCodeValid = await Promise.race([tryCatchWrapper(
|
||||
application,
|
||||
`let i = ${code.evaluations};` + code.candidate,
|
||||
), delay(timeout)]);
|
||||
|
||||
if (isCodeValid) {
|
||||
code.committed = code.candidate;
|
||||
@ -30,10 +37,11 @@ export const tryEvaluate = async (application: Editor, code: File, timeout = 500
|
||||
}
|
||||
}
|
||||
|
||||
export const evaluate = async (application: Editor, code: File, timeout = 5000): Promise<void> => {
|
||||
export const evaluate = async (application: Editor, code: File, timeout = 1000): Promise<void> => {
|
||||
try {
|
||||
await Promise.race([tryCatchWrapper(application, code.committed), delay(timeout)]);
|
||||
code.evaluations++;
|
||||
await Promise.race([tryCatchWrapper(application, code.committed as string), delay(timeout)]);
|
||||
if (code.evaluations)
|
||||
code.evaluations++;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ export class TransportNode extends AudioWorkletNode {
|
||||
this.$clock.innerHTML = `[${futureTimeStamp.bar} | ${futureTimeStamp.beat} | ${zeroPad(futureTimeStamp.pulse, '2')}]`;
|
||||
tryEvaluate(
|
||||
this.app,
|
||||
this.app.global_buffer
|
||||
this.app.global_buffer,
|
||||
);
|
||||
this.hasBeenEvaluated = true;
|
||||
this.currentPulsePosition = nextPulsePosition;
|
||||
|
||||
@ -427,7 +427,10 @@ export class Editor {
|
||||
}
|
||||
}
|
||||
});
|
||||
tryEvaluate(this, this.universes[this.selected_universe.toString()].init)
|
||||
tryEvaluate(
|
||||
this,
|
||||
this.universes[this.selected_universe.toString()].init,
|
||||
)
|
||||
}
|
||||
|
||||
get note_buffer() {
|
||||
@ -617,7 +620,9 @@ export class Editor {
|
||||
this.updateEditorView();
|
||||
|
||||
// Evaluating the initialisation script for the selected universe
|
||||
tryEvaluate(this, this.universes[this.selected_universe.toString()].init)
|
||||
tryEvaluate(this,
|
||||
this.universes[this.selected_universe.toString()].init,
|
||||
)
|
||||
}
|
||||
|
||||
openSettingsModal(): void {
|
||||
|
||||
Reference in New Issue
Block a user