Browse Source

KERNEL-12095 改下模板

es6
Austin.Duan 2 years ago
parent
commit
876fba6c43
  1. 43
      bin/cli/worker/template/main_thread.helper.t

43
bin/cli/worker/template/main_thread.helper.t

@ -3,25 +3,22 @@ import { ${WorkerName}MainThreadWorker } from './main_thread/${workerName}_main_
import { workerUrl } from 'fui-worker!./worker_thread/${workerName}_worker_thread'; import { workerUrl } from 'fui-worker!./worker_thread/${workerName}_worker_thread';
export class ${WorkerName}WorkerHelper { export class ${WorkerName}WorkerHelper {
public workers = new Map<string, ${WorkerName}MainThreadWorker>(); private worker: ${WorkerName}MainThreadWorker;
/** /**
* 支持创建多个worker * 拿到helper中的worker
* @param key worker key
*/ */
public createMainThreadWorker(key: string = '') { public get${WorkerName}MainThreadWorker() {
if (this.workers.has(key)) { if (this.worker) {
return this.workers.get(key); return this.worker;
} }
const newWorker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, { this.worker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, {
workerUrl: this.urlFormatter(workerUrl), workerUrl: this.urlFormatter(workerUrl),
workerName: BI.UUID(), workerName: BI.UUID(),
}); });
this.workers.set(key, newWorker); return this.worker;
return newWorker;
} }
/** /**
@ -33,30 +30,16 @@ export class ${WorkerName}WorkerHelper {
} }
/** /**
* 终止worker不传key就全部终止 * 终止worker
* @param key key
*/ */
public terminate(key?: string) { public terminate() {
if (BI.isNull(key)) { this.worker?.terminate();
this.workers.forEach(thread => {
thread.terminate();
});
this.workers.clear();
return;
}
this.workers.get(key)?.terminate();
this.workers.delete(key);
} }
} }
export const ${workerName}WorkerHelper = new ${WorkerName}WorkerHelper();
// 使用示例 // 使用示例
const worker = ${workerName}WorkerHelper.createMainThreadWorker(); const workerHelper = new ${WorkerName}WorkerHelper();
worker.testCommunication() workerHelper.get${WorkerName}MainThreadWorker()
.testCommunication()
.then(res => console.log(res)); .then(res => console.log(res));

Loading…
Cancel
Save