Browse Source

Pull request #2940: KERNEL-12095 worker主进程在helper层创建

Merge in VISUAL/fineui from ~AUSTIN.DUAN/fineui-visualization:master to master

* commit '3e43d5c9058422d13608d83f18c43f9a691f27fe':
  加下注释
  KERNEL-12095 改下模板
  更新下模板
  KERNEL-12095 WorkerHelper改下设计,支持校验、创建、终止worker
  KERNEL-12095 worker主进程在helper层创建
es6
Austin.Duan 2 years ago
parent
commit
1f439081e8
  1. 1
      bin/cli/worker/cli.worker.js
  2. 45
      bin/cli/worker/template/main_thread.helper.t
  3. 14
      bin/cli/worker/template/main_thread/main_thread.t

1
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'),
},
};

45
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));

14
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);
});

Loading…
Cancel
Save