diff --git a/bin/cli/worker/template/main_thread.helper.t b/bin/cli/worker/template/main_thread.helper.t index 29c679d87..e2242e27a 100644 --- a/bin/cli/worker/template/main_thread.helper.t +++ b/bin/cli/worker/template/main_thread.helper.t @@ -3,25 +3,22 @@ import { ${WorkerName}MainThreadWorker } from './main_thread/${workerName}_main_ import { workerUrl } from 'fui-worker!./worker_thread/${workerName}_worker_thread'; export class ${WorkerName}WorkerHelper { - public workers = new Map(); + private worker: ${WorkerName}MainThreadWorker; /** - * 支持创建多个worker - * @param key worker key + * 拿到helper中的worker */ - public createMainThreadWorker(key: string = '') { - if (this.workers.has(key)) { - return this.workers.get(key); + public get${WorkerName}MainThreadWorker() { + if (this.worker) { + return this.worker; } - const newWorker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, { + this.worker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, { workerUrl: this.urlFormatter(workerUrl), workerName: BI.UUID(), }); - this.workers.set(key, newWorker); - - return newWorker; + return this.worker; } /** @@ -33,30 +30,16 @@ export class ${WorkerName}WorkerHelper { } /** - * 终止worker,不传key就全部终止 - * @param key key + * 终止worker */ - public terminate(key?: string) { - if (BI.isNull(key)) { - this.workers.forEach(thread => { - thread.terminate(); - }); - - this.workers.clear(); - - return; - } - - this.workers.get(key)?.terminate(); - - this.workers.delete(key); + public terminate() { + this.worker?.terminate(); } } -export const ${workerName}WorkerHelper = new ${WorkerName}WorkerHelper(); - // 使用示例 -const worker = ${workerName}WorkerHelper.createMainThreadWorker(); +const workerHelper = new ${WorkerName}WorkerHelper(); -worker.testCommunication() +workerHelper.get${WorkerName}MainThreadWorker() + .testCommunication() .then(res => console.log(res));