Add code documentation
This commit is contained in:
@ -8,11 +8,19 @@ const codeReplace = (code: string): string => {
|
||||
|
||||
const tryCatchWrapper = async (
|
||||
application: Editor,
|
||||
code: string,
|
||||
code: string
|
||||
): Promise<boolean> => {
|
||||
/**
|
||||
* Wraps the provided code in a try-catch block and executes it.
|
||||
*
|
||||
* @param application - The editor application.
|
||||
* @param code - The code to be executed.
|
||||
* @returns A promise that resolves to a boolean indicating whether the code executed successfully or not.
|
||||
*/
|
||||
|
||||
try {
|
||||
await new Function(`"use strict"; ${codeReplace(code)}`).call(
|
||||
application.api,
|
||||
application.api
|
||||
);
|
||||
return true;
|
||||
} catch (error) {
|
||||
@ -26,18 +34,32 @@ const cache = new Map<string, Function>();
|
||||
const MAX_CACHE_SIZE = 40;
|
||||
|
||||
const addFunctionToCache = (code: string, fn: Function) => {
|
||||
/**
|
||||
* Adds a function to the cache.
|
||||
* @param code - The code associated with the function.
|
||||
* @param fn - The function to be added to the cache.
|
||||
*/
|
||||
if (cache.size >= MAX_CACHE_SIZE) {
|
||||
cache.delete(cache.keys().next().value);
|
||||
}
|
||||
cache.set(code, fn);
|
||||
};
|
||||
|
||||
// Optimized evaluate function with reduced complexity
|
||||
export const tryEvaluate = async (
|
||||
application: Editor,
|
||||
code: File,
|
||||
timeout = 5000,
|
||||
timeout = 5000
|
||||
): Promise<void> => {
|
||||
/**
|
||||
* Tries to evaluate the provided code within a specified timeout period.
|
||||
* Increments the evaluation count of the code file.
|
||||
* If the code is valid, updates the committed code and adds the evaluated function to the cache.
|
||||
* If the code is invalid, retries the evaluation.
|
||||
* @param application - The editor application.
|
||||
* @param code - The code file to evaluate.
|
||||
* @param timeout - The timeout period in milliseconds (default: 5000).
|
||||
* @returns A Promise that resolves when the evaluation is complete.
|
||||
*/
|
||||
code.evaluations!++;
|
||||
const candidateCode = code.candidate;
|
||||
|
||||
@ -55,7 +77,7 @@ export const tryEvaluate = async (
|
||||
if (isCodeValid) {
|
||||
code.committed = code.candidate;
|
||||
const newFunction = new Function(
|
||||
`"use strict"; ${codeReplace(wrappedCode)}`,
|
||||
`"use strict"; ${codeReplace(wrappedCode)}`
|
||||
);
|
||||
addFunctionToCache(candidateCode, newFunction);
|
||||
} else {
|
||||
@ -71,8 +93,16 @@ export const tryEvaluate = async (
|
||||
export const evaluate = async (
|
||||
application: Editor,
|
||||
code: File,
|
||||
timeout = 1000,
|
||||
timeout = 1000
|
||||
): Promise<void> => {
|
||||
/**
|
||||
* Evaluates the given code using the provided application and timeout.
|
||||
* @param application The editor application.
|
||||
* @param code The code file to evaluate.
|
||||
* @param timeout The timeout value in milliseconds (default: 1000).
|
||||
* @returns A Promise that resolves when the evaluation is complete.
|
||||
*/
|
||||
|
||||
try {
|
||||
await Promise.race([
|
||||
tryCatchWrapper(application, code.committed as string),
|
||||
@ -87,7 +117,7 @@ export const evaluate = async (
|
||||
|
||||
export const evaluateOnce = async (
|
||||
application: Editor,
|
||||
code: string,
|
||||
code: string
|
||||
): Promise<void> => {
|
||||
/**
|
||||
* Evaluates the code once without any caching or error-handling mechanisms besides the tryCatchWrapper.
|
||||
|
||||
Reference in New Issue
Block a user