create miaoworkerpool.js
This commit is contained in:
1 parent
133d0b7844
commit
17caf491f1
3 files changed
+2239
No files matched your search
@@ -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": {
|
||||
|
||||
Generated
+2196
File diff suppressed because it is too large.
Load diff
@@ -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
|
||||
Reference in new issue
Block a user