desperate commit
This commit is contained in:
@ -6,22 +6,14 @@ const delay = (ms: number) =>
|
|||||||
setTimeout(() => reject(new Error("Operation took too long")), ms)
|
setTimeout(() => reject(new Error("Operation took too long")), ms)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
const tryCatchWrapper = (
|
const tryCatchWrapper = (
|
||||||
application: Editor,
|
application: Editor,
|
||||||
code: string
|
code: string
|
||||||
): Promise<boolean> => {
|
): 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
|
|
||||||
* being evaluated.
|
|
||||||
*
|
|
||||||
* @param application - The main application instance
|
|
||||||
* @param code - The string of code to wrap and evaluate
|
|
||||||
* @returns A promise that resolves to true if the code is valid and false if the code is invalid
|
|
||||||
*/
|
|
||||||
return new Promise((resolve, _) => {
|
return new Promise((resolve, _) => {
|
||||||
try {
|
try {
|
||||||
Function(`"use strict";try{${code}} catch (e) {console.log(e)};`).call(application.api);
|
Function(`"use strict";try{${code}} catch (e) {console.log(e)};`).call(application.api);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
@ -30,35 +22,46 @@ const tryCatchWrapper = (
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const cache = new Map<string, Function>();
|
||||||
|
const MAX_CACHE_SIZE = 20;
|
||||||
|
|
||||||
|
const addFunctionToCache = (code: string, fn: Function) => {
|
||||||
|
if (cache.size >= MAX_CACHE_SIZE) {
|
||||||
|
// Delete the first item if cache size exceeds max size
|
||||||
|
cache.delete(cache.keys().next().value);
|
||||||
|
}
|
||||||
|
cache.set(code, fn);
|
||||||
|
}
|
||||||
|
|
||||||
export const tryEvaluate = async (
|
export const tryEvaluate = async (
|
||||||
/**
|
|
||||||
* This function attempts to evaluate a string of code in the context of user API.
|
|
||||||
* If the code is invalid, it will attempt to evaluate the previous valid code.
|
|
||||||
*
|
|
||||||
* @param application - The main application instance
|
|
||||||
* @param code - The set of files to evaluate
|
|
||||||
* @param timeout - The timeout in milliseconds
|
|
||||||
* @returns A promise that resolves to void
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
application: Editor,
|
application: Editor,
|
||||||
code: File,
|
code: File,
|
||||||
timeout = 5000
|
timeout = 5000
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
code.evaluations!++;
|
code.evaluations!++;
|
||||||
const isCodeValid = await Promise.race([
|
const candidateCode = code.candidate;
|
||||||
tryCatchWrapper(
|
|
||||||
application,
|
if (cache.has(candidateCode)) {
|
||||||
(`let i = ${code.evaluations};` + code.candidate) as string
|
// If the code is already in cache, use it
|
||||||
),
|
cache.get(candidateCode)!.call(application.api);
|
||||||
delay(timeout),
|
console.log('Using cached code')
|
||||||
]);
|
|
||||||
|
|
||||||
if (isCodeValid) {
|
|
||||||
code.committed = code.candidate;
|
|
||||||
} else {
|
} else {
|
||||||
await evaluate(application, code, timeout);
|
console.log('Evaluating code')
|
||||||
|
const wrappedCode = `let i = ${code.evaluations};` + candidateCode;
|
||||||
|
// Otherwise, evaluate the code and if valid, add it to the cache
|
||||||
|
const isCodeValid = await Promise.race([
|
||||||
|
tryCatchWrapper(application, wrappedCode as string),
|
||||||
|
delay(timeout),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (isCodeValid) {
|
||||||
|
code.committed = code.candidate;
|
||||||
|
const newFunction = new Function(`"use strict";try{${wrappedCode}} catch (e) {console.log(e)};`);
|
||||||
|
addFunctionToCache(candidateCode, newFunction);
|
||||||
|
} else {
|
||||||
|
await evaluate(application, code, timeout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
@ -70,14 +73,6 @@ export const evaluate = async (
|
|||||||
code: File,
|
code: File,
|
||||||
timeout = 1000
|
timeout = 1000
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
/**
|
|
||||||
* This function evaluates a string of code in the context of user API.
|
|
||||||
*
|
|
||||||
* @param application - The main application instance
|
|
||||||
* @param code - The set of files to evaluate
|
|
||||||
* @param timeout - The timeout in milliseconds
|
|
||||||
* @returns A promise that resolves to void
|
|
||||||
*/
|
|
||||||
try {
|
try {
|
||||||
await Promise.race([
|
await Promise.race([
|
||||||
tryCatchWrapper(application, code.committed as string),
|
tryCatchWrapper(application, code.committed as string),
|
||||||
|
|||||||
@ -14,7 +14,12 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
this.currentPulsePosition = 0;
|
this.currentPulsePosition = 0;
|
||||||
this.nextPulsePosition = -1;
|
this.nextPulsePosition = -1;
|
||||||
this.executionLatency = 0;
|
this.executionLatency = 0;
|
||||||
this.lastLatencies = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
this.lastLatencies = [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
];
|
||||||
this.indexOfLastLatencies = 0;
|
this.indexOfLastLatencies = 0;
|
||||||
// setInterval(() => this.ping(), 1000);
|
// setInterval(() => this.ping(), 1000);
|
||||||
this.startTime = null;
|
this.startTime = null;
|
||||||
@ -35,17 +40,12 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
if (this.nextPulsePosition !== nextPulsePosition) {
|
if (this.nextPulsePosition !== nextPulsePosition) {
|
||||||
this.nextPulsePosition = nextPulsePosition;
|
this.nextPulsePosition = nextPulsePosition;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// const now = performance.now(); // Use AudioContext time instead
|
|
||||||
const now = this.app.audioContext.currentTime;
|
const now = this.app.audioContext.currentTime;
|
||||||
this.app.clock.time_position = futureTimeStamp;
|
this.app.clock.time_position = futureTimeStamp;
|
||||||
// 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.global_buffer);
|
||||||
this.app,
|
|
||||||
this.app.global_buffer,
|
|
||||||
);
|
|
||||||
this.hasBeenEvaluated = true;
|
this.hasBeenEvaluated = true;
|
||||||
this.currentPulsePosition = nextPulsePosition;
|
this.currentPulsePosition = nextPulsePosition;
|
||||||
// const then = performance.now(); // Use AudioContext time instead
|
|
||||||
const then = this.app.audioContext.currentTime;
|
const then = this.app.audioContext.currentTime;
|
||||||
this.lastLatencies[this.indexOfLastLatencies] = then - now;
|
this.lastLatencies[this.indexOfLastLatencies] = then - now;
|
||||||
this.indexOfLastLatencies = (this.indexOfLastLatencies + 1) % this.lastLatencies.length;
|
this.indexOfLastLatencies = (this.indexOfLastLatencies + 1) % this.lastLatencies.length;
|
||||||
|
|||||||
Reference in New Issue
Block a user