create miaoworkerpool.js

This commit is contained in:
539943419 committed 2024-06-29 01:06:00 +08:00
1 parent 133d0b7844
commit 17caf491f1
3 files changed
+2239

No files matched your search

+1
View File
@@ -27,6 +27,7 @@
"vue-clipboard3": "^2.0.0",
"vue-router": "^4.2.5",
"websocket-heartbeat-js": "^1.1.3",
"workerpool": "^9.1.3",
"ws": "^8.16.0"
},
"devDependencies": {
+2196
View File
File diff suppressed because it is too large. Load diff
+42
View File
@@ -0,0 +1,42 @@
import workerpool from 'workerpool'
function randName(){
return `randName${Math.ceil(Math.random()*100000)}`
}
class MiaoWorkerPool{
constructor(){
this.workers = {}
this.pool = workerpool.pool()
}
/**
* Description 添加function
* @param {function} func 待添加function
* @returns {String} 返回添加的function名
*/
init(func){
let funcName = func.name.length > 0 ? func.name : randName();
this[funcName] = async (...args)=>{
try{
let result = await this.pool.exec(func,args)
return {
isSuccess:true,
result
}
}catch(e){
throw {
isSuccess:false,
result:e
}
}
}
return funcName
}
}
export default MiaoWorkerPool