mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-12-31 23:09:09 +08:00
* feat: pnpm new * Refactor build and release workflows, update dependencies Switch build scripts and workflows from npm to pnpm, update build and artifact paths, and simplify release workflow by removing version detection and changelog steps. Add new dependencies (silk-wasm, express, ws, node-pty-prebuilt-multiarch), update exports in package.json files, and add vite config for napcat-framework. Also, rename manifest.json for framework package and fix static asset copying in shell build config.
35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import { Worker } from 'worker_threads';
|
|
|
|
export async function runTask<T, R> (workerScript: string, taskData: T): Promise<R> {
|
|
const worker = new Worker(workerScript);
|
|
try {
|
|
return await new Promise<R>((resolve, reject) => {
|
|
worker.on('message', (result: R) => {
|
|
if ((result as any)?.log) {
|
|
console.error('Worker Log--->:', (result as { log: string }).log);
|
|
}
|
|
if ((result as any)?.error) {
|
|
reject(new Error('Worker error: ' + (result as { error: string }).error));
|
|
}
|
|
resolve(result);
|
|
});
|
|
|
|
worker.on('error', (error) => {
|
|
reject(new Error(`Worker error: ${error.message}`));
|
|
});
|
|
|
|
worker.on('exit', (code) => {
|
|
if (code !== 0) {
|
|
reject(new Error(`Worker stopped with exit code ${code}`));
|
|
}
|
|
});
|
|
worker.postMessage(taskData);
|
|
});
|
|
} catch (error: unknown) {
|
|
throw new Error(`Failed to run task: ${(error as Error).message}`);
|
|
} finally {
|
|
// Ensure the worker is terminated after the promise is settled
|
|
worker.terminate();
|
|
}
|
|
}
|