diff --git a/bin/cli/worker/cli.worker.js b/bin/cli/worker/cli.worker.js index cfd331c8b..555c99cf0 100644 --- a/bin/cli/worker/cli.worker.js +++ b/bin/cli/worker/cli.worker.js @@ -56,6 +56,7 @@ module.exports = { }, [`${name}_worker_thread.ts`]: path.resolve(__dirname, './template/worker_thread/worker_thread.t'), }, + [`${name}_main_thread.helper.ts`]: path.resolve(__dirname, './template/main_thread.helper.t'), }, }; diff --git a/bin/cli/worker/template/main_thread.helper.t b/bin/cli/worker/template/main_thread.helper.t new file mode 100644 index 000000000..af02e5192 --- /dev/null +++ b/bin/cli/worker/template/main_thread.helper.t @@ -0,0 +1,45 @@ +import { ${WorkerName}MainThreadWorker } from './main_thread/${workerName}_main_thread'; +// 不需要一起打包的话则不需要引入这行 +import { workerUrl } from 'fui-worker!./worker_thread/${workerName}_worker_thread'; + +export class ${WorkerName}WorkerHelper { + private worker: ${WorkerName}MainThreadWorker; + + /** + * 拿到helper中的worker + */ + public getWorker() { + if (this.worker) { + return this.worker; + } + + this.worker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, { + workerUrl: this.urlFormatter(workerUrl), + workerName: BI.UUID(), + }); + + return this.worker; + } + + /** + * 格式化worker url,比如补充一些环境信息到参数里 + * @param url worker url + */ + private urlFormatter(url: string) { + return url; + } + + /** + * 终止worker + */ + public terminate() { + this.worker?.terminate(); + } +} + +// 使用示例 +// const workerHelper = new ${WorkerName}WorkerHelper(); + +// workerHelper.getWorker() +// .testCommunication() +// .then(res => console.log(res)); 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); - }); diff --git a/package.json b/package.json index a272024af..fe7f49173 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fineui", - "version": "2.0.20220726155349", + "version": "2.0.20220727111551", "description": "fineui", "main": "dist/fineui_without_conflict.min.js", "types": "dist/lib/index.d.ts",