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