Browse Source

更新下模板

es6
Austin.Duan 2 years ago
parent
commit
17049a18f9
  1. 37
      bin/cli/worker/template/main_thread.helper.t

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

@ -3,26 +3,23 @@ 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: WorkerItems[] = []; public workers = new Map<string, ${WorkerName}MainThreadWorker>();
/** /**
* 支持创建多个worker * 支持创建多个worker
* @param key worker key * @param key worker key
*/ */
public createMainThreadWorker(key: string = '') { public createMainThreadWorker(key: string = '') {
const findItem = this.workers.find(item => item.key === key); if (this.workers.has(key)) {
return this.workers.get(key);
if (findItem) return findItem.worker; }
const newWorker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, { const newWorker = BI.Workers.createWorker(${WorkerName}MainThreadWorker, {
workerUrl: this.urlFormatter(workerUrl), workerUrl: this.urlFormatter(workerUrl),
workerName: BI.UUID(), workerName: BI.UUID(),
}); });
this.workers.push({ this.workers.set(key, newWorker);
key,
worker: newWorker,
});
return newWorker; return newWorker;
} }
@ -40,32 +37,26 @@ export class ${WorkerName}WorkerHelper {
* @param key key * @param key key
*/ */
public terminate(key?: string) { public terminate(key?: string) {
if (!key) { if (BI.isNull(key)) {
this.workers.forEach(item => { this.workers.forEach(thread => {
item.worker.terminate(); thread.terminate();
}); });
this.workers = []; this.workers.clear();
return; return;
} }
this.workers.find(item => item.key === key)?.worker.terminate();
const findIndex = this.workers.findIndex(item => item.key === key); this.workers.get(key)?.terminate();
findIndex !== -1 this.workers.delete(key);
&& this.workers.splice(findIndex, 1);
} }
} }
export const ${workerName}WorkerHelper = new ${WorkerName}WorkerHelper(); export const ${workerName}WorkerHelper = new ${WorkerName}WorkerHelper();
interface WorkerItems {
key: string;
worker: ${WorkerName}MainThreadWorker;
}
// 使用示例 // 使用示例
${workerName}WorkerHelper.createMainThreadWorker() const worker = ${workerName}WorkerHelper.createMainThreadWorker();
.testCommunication()
worker.testCommunication()
.then(res => console.log(res)); .then(res => console.log(res));

Loading…
Cancel
Save