fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

import { ${WorkerName}MainThreadWorker } from './main_thread/${workerName}_main_thread';
// 不需要一起打包的话则不需要引入这行
import { workerUrl } from 'fui-worker!./worker_thread/${workerName}_worker_thread';
export class ${WorkerName}WorkerHelper {
public workers: WorkerItems[] = [];
/**
* 支持创建多个worker
* @param key worker key
*/
public createMainThreadWorker(key: string = '') {
const findItem = this.workers.find(item => item.key === key);
if (findItem) return findItem.worker;
const newWorker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, {
workerUrl: this.urlFormatter(workerUrl),
workerName: BI.UUID(),
});
this.workers.push({
key,
worker: newWorker,
});
return newWorker;
}
/**
* 格式化worker url比如补充一些环境信息到参数里
* @param url worker url
*/
private urlFormatter(url: string) {
return url;
}
/**
* 终止worker不传key就全部终止
* @param key key
*/
public terminate(key?: string) {
if (!key) {
this.workers.forEach(item => {
item.worker.terminate();
});
this.workers = [];
return;
}
this.workers.find(item => item.key === key)?.worker.terminate();
const findIndex = this.workers.findIndex(item => item.key === key);
findIndex !== -1
&& this.workers.splice(findIndex, 1);
}
}
export const ${workerName}WorkerHelper = new ${WorkerName}WorkerHelper();
interface WorkerItems {
key: string;
worker: ${WorkerName}MainThreadWorker;
}
// 使用示例
${workerName}WorkerHelper.createMainThreadWorker()
.testCommunication()
.then(res => console.log(res));