新增worker辅助类

This commit is contained in:
yhh
2021-01-29 16:16:44 +08:00
parent 11d03cef0c
commit b9822f2270

View File

@@ -0,0 +1,45 @@
module es {
/**
* 开辟一个新线程
* 注意:它无法获得主线程中的上下文
*/
export class WorkerUtils {
/** 正在执行的队列 */
private static readonly pendingJobs = {};
private static jobIdGen = 0;
/**
* 创建一个worker
* @param doFunc worker所能做的事情
*
* @example const worker = es.WorkerUtils.makeWorker(()=>{
* onmessage = ({data: {jobId, meesage}}) => {
* // worker内做的事
* console.log('我是线程', message, jobId);
* };
* });
*
* worker('主线程发送消息').then(message => {
* console.log('主线程收到消息', message);
* });
*/
public static makeWorker(doFunc: Function) {
const worker = new Worker(URL.createObjectURL(new Blob([`(${doFunc.toString()})()`])));
worker.onmessage = ({ data: { result, jobId } }) => {
if (typeof this.pendingJobs[jobId] == 'function')
this.pendingJobs[jobId](result);
delete this.pendingJobs[jobId];
};
return (...message: any[]) => {
return new Promise(resolve => {
const jobId = this.jobIdGen++;
this.pendingJobs[jobId] = resolve;
worker.postMessage({ jobId, message });
});
}
}
}
}