Browse Source

KERNEL-12095 WorkerHelper改下设计,支持校验、创建、终止worker

es6
Austin.Duan 2 years ago
parent
commit
2cb037c669
  1. 2
      bin/cli/worker/cli.worker.js
  2. 71
      bin/cli/worker/template/main_thread.helper.t
  3. 26
      bin/cli/worker/template/main_thread/main_thread.helper.t

2
bin/cli/worker/cli.worker.js

@ -45,7 +45,6 @@ module.exports = {
'action.worker_ability_test.ts': path.resolve(__dirname, './template/main_thread/action/action.worker_ability_test.t'),
},
[`${name}_main_thread.ts`]: path.resolve(__dirname, './template/main_thread/main_thread.t'),
[`${name}_main_thread.helper.ts`]: path.resolve(__dirname, './template/main_thread/main_thread.helper.t'),
},
utils: {
'action_type.ts': path.resolve(__dirname, './template/utils/action_type.t'),
@ -57,6 +56,7 @@ module.exports = {
},
[`${name}_worker_thread.ts`]: path.resolve(__dirname, './template/worker_thread/worker_thread.t'),
},
[`${name}_main_thread.helper.ts`]: path.resolve(__dirname, './template/main_thread.helper.t'),
},
};

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

@ -0,0 +1,71 @@
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));

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

@ -1,26 +0,0 @@
import { ${WorkerName}MainThreadWorker } from './new_main_thread';
// 不需要一起打包的话则不需要引入这行
import { workerUrl } from 'fui-worker!../worker_thread/new_worker_thread';
// MainThread的创建在helper层处理
export class ${WorkerName}MainThreadWorkerHelper {
public createMainTreadWorker() {
return BI.Workers.createWorker(${WorkerName}MainThreadWorker, {
workerUrl: this.urlFormatter(workerUrl),
workerName: BI.UUID(),
});
}
private urlFormatter(v: string) {
return v;
}
}
export const mainThreadWorkerHelper = new ${WorkerName}MainThreadWorkerHelper();
// 调用示例
mainThreadWorkerHelper.createMainTreadWorker()
.testCommunication()
.then(res => {
console.log(res)
})
Loading…
Cancel
Save