|
|
|
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 get${WorkerName}MainThreadWorker() {
|
|
|
|
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.get${WorkerName}MainThreadWorker()
|
|
|
|
.testCommunication()
|
|
|
|
.then(res => console.log(res));
|