Introducing the magical I variable

This commit is contained in:
2023-08-04 10:43:37 +02:00
parent beecf49344
commit 44ee91a433
5 changed files with 45 additions and 33 deletions

View File

@ -39,6 +39,7 @@
<path stroke-linecap="round" stroke-linejoin="round" d="M12 6.042A8.967 8.967 0 006 3.75c-1.052 0-2.062.18-3 .512v14.25A8.987 8.987 0 016 18c2.305 0 4.408.867 6 2.292m0-14.25a8.966 8.966 0 016-2.292c1.052 0 2.062.18 3 .512v14.25A8.987 8.987 0 0018 18a8.967 8.967 0 00-6 2.292m0-14.25v14.25" /> <path stroke-linecap="round" stroke-linejoin="round" d="M12 6.042A8.967 8.967 0 006 3.75c-1.052 0-2.062.18-3 .512v14.25A8.987 8.987 0 016 18c2.305 0 4.408.867 6 2.292m0-14.25a8.966 8.966 0 016-2.292c1.052 0 2.062.18 3 .512v14.25A8.987 8.987 0 0018 18a8.967 8.967 0 00-6 2.292m0-14.25v14.25" />
</svg> </svg>
<span id="universe-viewer" class="ml-4 text-2xl text-white">Topos</span> <span id="universe-viewer" class="ml-4 text-2xl text-white">Topos</span>
</a> </a>
<nav class="py-0 flex flex-wrap items-center text-base absolute right-0"> <nav class="py-0 flex flex-wrap items-center text-base absolute right-0">
<!-- <!--

View File

@ -5,10 +5,10 @@ import { MidiConnection } from "./IO/MidiConnection";
// @ts-ignore // @ts-ignore
import { webaudioOutput, samples } from '@strudel.cycles/webaudio'; import { webaudioOutput, samples } from '@strudel.cycles/webaudio';
// const sound = (value: any) => ({ const sound = (value: any) => ({
// value, context: {}, value, context: {},
// ensureObjectValue: () => {} ensureObjectValue: () => {}
// }); });
class DrunkWalk { class DrunkWalk {
public min: number; public min: number;
@ -66,7 +66,7 @@ export class UserAPI {
load: samples load: samples
constructor (public app: Editor) { 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 { script(...args: number[]): void {
args.forEach(arg => { args.forEach(arg => {
tryEvaluate(this.app, this.app.universes[ tryEvaluate(
this.app.selected_universe].locals[arg]) this.app,
this.app.universes[this.app.selected_universe].locals[arg],
)
}) })
} }
s = this.script s = this.script
@ -314,19 +316,15 @@ export class UserAPI {
// ============================================================= // =============================================================
get i() { return this.app.universes[this.app.selected_universe].global.evaluations } 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 e1() { return this.app.universes[this.app.selected_universe].locals[1].evaluations }
get e2() { 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[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[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[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[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[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[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[8].evaluations } get e9() { return this.app.universes[this.app.selected_universe].locals[9].evaluations }
public evaluations_number(index: number): number {
return this.app.universes[this.app.selected_universe].locals[index].evaluations
}
e = this.evaluations_number
// ============================================================= // =============================================================
// Time markers // Time markers
@ -416,7 +414,7 @@ export class UserAPI {
// Trivial functions // Trivial functions
// ============================================================= // =============================================================
// sound = async (values: object) => { sound = async (values: object) => {
// webaudioOutput(sound(values), 0.00) webaudioOutput(sound(values), 0.00)
// } }
} }

View File

@ -1,10 +1,10 @@
import type { Editor } from './main'; import type { Editor } from './main';
import type { File } from './AppSettings'; 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> => { const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> => {
return new Promise((resolve, reject) => { return new Promise((resolve, _) => {
try { try {
Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(application.api); Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(application.api);
resolve(true); 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 { try {
const isCodeValid = await Promise.race([tryCatchWrapper(application, code.candidate), delay(timeout)]);
code.evaluations++; code.evaluations++;
const isCodeValid = await Promise.race([tryCatchWrapper(
application,
`let i = ${code.evaluations};` + code.candidate,
), delay(timeout)]);
if (isCodeValid) { if (isCodeValid) {
code.committed = code.candidate; 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 { try {
await Promise.race([tryCatchWrapper(application, code.committed), delay(timeout)]); await Promise.race([tryCatchWrapper(application, code.committed as string), delay(timeout)]);
code.evaluations++; if (code.evaluations)
code.evaluations++;
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }

View File

@ -47,7 +47,7 @@ export class TransportNode extends AudioWorkletNode {
this.$clock.innerHTML = `[${futureTimeStamp.bar} | ${futureTimeStamp.beat} | ${zeroPad(futureTimeStamp.pulse, '2')}]`; this.$clock.innerHTML = `[${futureTimeStamp.bar} | ${futureTimeStamp.beat} | ${zeroPad(futureTimeStamp.pulse, '2')}]`;
tryEvaluate( tryEvaluate(
this.app, this.app,
this.app.global_buffer this.app.global_buffer,
); );
this.hasBeenEvaluated = true; this.hasBeenEvaluated = true;
this.currentPulsePosition = nextPulsePosition; this.currentPulsePosition = nextPulsePosition;

View File

@ -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() { get note_buffer() {
@ -617,7 +620,9 @@ export class Editor {
this.updateEditorView(); this.updateEditorView();
// Evaluating the initialisation script for the selected universe // 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 { openSettingsModal(): void {