From ed92dda2a5b561d0d00eb76a9986e0417d165167 Mon Sep 17 00:00:00 2001 From: "Austin.Duan" Date: Mon, 25 Jul 2022 16:17:15 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-12095=20worker=E4=B8=BB=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E5=9C=A8helper=E5=B1=82=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/cli/worker/cli.worker.js | 1 + .../template/main_thread/main_thread.helper.t | 26 +++++++++++++++++++ .../worker/template/main_thread/main_thread.t | 14 +--------- 3 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 bin/cli/worker/template/main_thread/main_thread.helper.t diff --git a/bin/cli/worker/cli.worker.js b/bin/cli/worker/cli.worker.js index cfd331c8b..02a7c803a 100644 --- a/bin/cli/worker/cli.worker.js +++ b/bin/cli/worker/cli.worker.js @@ -45,6 +45,7 @@ module.exports = { 'action.worker_ability_test.ts': path.resolve(__dirname, './template/main_thread/action/action.worker_ability_test.t'), }, [`${name}_main_thread.ts`]: path.resolve(__dirname, './template/main_thread/main_thread.t'), + [`${name}_main_thread.helper.ts`]: path.resolve(__dirname, './template/main_thread/main_thread.helper.t'), }, utils: { 'action_type.ts': path.resolve(__dirname, './template/utils/action_type.t'), diff --git a/bin/cli/worker/template/main_thread/main_thread.helper.t b/bin/cli/worker/template/main_thread/main_thread.helper.t new file mode 100644 index 000000000..defe01502 --- /dev/null +++ b/bin/cli/worker/template/main_thread/main_thread.helper.t @@ -0,0 +1,26 @@ +import { ${WorkerName}MainThreadWorker } from './new_main_thread'; +// 不需要一起打包的话则不需要引入这行 +import { workerUrl } from 'fui-worker!../worker_thread/new_worker_thread'; + +// MainThread的创建在helper层处理 +export class ${WorkerName}MainThreadWorkerHelper { + public createMainTreadWorker() { + return BI.Workers.createWorker(${WorkerName}MainThreadWorker, { + workerUrl: this.urlFormatter(workerUrl), + workerName: BI.UUID(), + }); + } + + private urlFormatter(v: string) { + return v; + } +} + +export const mainThreadWorkerHelper = new ${WorkerName}MainThreadWorkerHelper(); + +// 调用示例 +mainThreadWorkerHelper.createMainTreadWorker() + .testCommunication() + .then(res => { + console.log(res) + }) \ No newline at end of file diff --git a/bin/cli/worker/template/main_thread/main_thread.t b/bin/cli/worker/template/main_thread/main_thread.t index 75f8fb674..00fb09177 100644 --- a/bin/cli/worker/template/main_thread/main_thread.t +++ b/bin/cli/worker/template/main_thread/main_thread.t @@ -1,8 +1,6 @@ import { WorkerAbilityTestMainThreadAction } from './action/action.worker_ability_test'; -// 不需要一起打包的话则不需要引入这行 -import { workerUrl } from 'fui-worker!../worker_thread/${workerName}_worker_thread'; -class ${WorkerName}MainTreadWorker extends BI.Workers.MainThreadWorker { +export class ${WorkerName}MainThreadWorker extends BI.Workers.MainThreadWorker { private communicationTest: WorkerAbilityTestMainThreadAction; public initActions(): void { @@ -13,13 +11,3 @@ class ${WorkerName}MainTreadWorker extends BI.Workers.MainThreadWorker { return this.communicationTest.communicationTest(); } } - -const ${workerName}MainTreadWorker = BI.Workers.createWorker(${WorkerName}MainTreadWorker, { - workerUrl, - workerName: BI.UUID(), -}); - -${workerName}MainTreadWorker.testCommunication() - .then(v => { - console.log(v); - });