A
This commit is contained in:
53
webui/src/lib/runner.ts
Normal file
53
webui/src/lib/runner.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
"use client";
|
||||
|
||||
let worker: Worker | null = null;
|
||||
|
||||
export async function runScript(
|
||||
script: string,
|
||||
appendOutput: (line: string) => void,
|
||||
appendTerminal?: (line: string) => void
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (worker) {
|
||||
worker.terminate();
|
||||
}
|
||||
|
||||
worker = new Worker(new URL('./script-runner.worker.ts', import.meta.url));
|
||||
|
||||
worker.onmessage = (event) => {
|
||||
const { type, line, error } = event.data;
|
||||
|
||||
if (type === 'output') {
|
||||
appendOutput(line);
|
||||
} else if (type === 'terminal') {
|
||||
appendTerminal?.(line);
|
||||
} else if (type === 'complete') {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
resolve();
|
||||
} else if (type === 'error') {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
reject(new Error(error));
|
||||
} else if (type === 'stopped') {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
worker.onerror = (error) => {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
reject(error);
|
||||
};
|
||||
|
||||
worker.postMessage({ type: 'run', script });
|
||||
});
|
||||
}
|
||||
|
||||
export function stopScript(): void {
|
||||
if (worker) {
|
||||
worker.postMessage({ type: 'stop' });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user