From 876fba6c4392824ad47e4d3ba3e744a9ab1f04b1 Mon Sep 17 00:00:00 2001 From: "Austin.Duan" Date: Wed, 27 Jul 2022 10:21:31 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-12095=20=E6=94=B9=E4=B8=8B=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/cli/worker/template/main_thread.helper.t | 43 ++++++-------------- 1 file changed, 13 insertions(+), 30 deletions(-) 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));