3 lines
20 KiB
JavaScript
3 lines
20 KiB
JavaScript
|
/*! For license information please see workerpool.min.js.LICENSE.txt */
|
||
|
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("workerpool",[],r):"object"==typeof exports?exports.workerpool=r():e.workerpool=r()}("undefined"!=typeof self?self:this,(function(){return function(){var __webpack_modules__={345:function(e,r,t){var o=t(219),n=t(751),i=t(828),s=new(t(833));function u(e,r){"string"==typeof e?this.script=e||null:(this.script=null,r=e),this.workers=[],this.tasks=[],r=r||{},this.forkArgs=r.forkArgs||[],this.forkOpts=r.forkOpts||{},this.debugPortStart=r.debugPortStart||43210,this.nodeWorker=r.nodeWorker,this.workerType=r.workerType||r.nodeWorker||"auto",this.maxQueueSize=r.maxQueueSize||1/0,r&&"maxWorkers"in r?(function(e){if(!c(e)||!a(e)||e<1)throw new TypeError("Option maxWorkers must be an integer number >= 1")}(r.maxWorkers),this.maxWorkers=r.maxWorkers):this.maxWorkers=Math.max((i.cpus||4)-1,1),r&&"minWorkers"in r&&("max"===r.minWorkers?this.minWorkers=this.maxWorkers:(function(e){if(!c(e)||!a(e)||e<0)throw new TypeError("Option minWorkers must be an integer number >= 0")}(r.minWorkers),this.minWorkers=r.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)),this._ensureMinWorkers()),this._boundNext=this._next.bind(this),"thread"===this.workerType&&n.ensureWorkerThreads()}function c(e){return"number"==typeof e}function a(e){return Math.round(e)==e}u.prototype.exec=function(e,r,t){if(r&&!Array.isArray(r))throw new TypeError('Array expected as argument "params"');if("string"==typeof e){var n=o.defer();if(this.tasks.length>=this.maxQueueSize)throw new Error("Max queue size of "+this.maxQueueSize+" reached");var i=this.tasks,s={method:e,params:r,resolver:n,timeout:null,options:t};i.push(s);var u=n.promise.timeout;return n.promise.timeout=function(e){return-1!==i.indexOf(s)?(s.timeout=e,n.promise):u.call(n.promise,e)},this._next(),n.promise}if("function"==typeof e)return this.exec("run",[String(e),r]);throw new TypeError('Function or string expected as argument "method"')},u.prototype.proxy=function(){if(arguments.length>0)throw new Error("No arguments expected");var e=this;return this.exec("methods").then((function(r){var t={};return r.forEach((function(r){t[r]=function(){return e.exec(r,Array.prototype.slice.call(arguments))}})),t}))},u.prototype._next=function(){if(this.tasks.length>0){var e=this._getWorker();if(e){var r=this,t=this.tasks.shift();if(t.resolver.promise.pending){var o=e.exec(t.method,t.params,t.resolver,t.options).then(r._boundNext).catch((function(){if(e.terminated)return r._removeWorker(e)})).then((function(){r._next()}));"number"==typeof t.timeout&&o.timeout(t.timeout)}else r._next()}}},u.prototype._getWorker=function(){for(var e=this.workers,r=0;r<e.length;r++){var t=e[r];if(!1===t.busy())return t}return e.length<this.maxWorkers?(t=this._createWorkerHandler(),e.push(t),t):null},u.prototype._removeWorker=function(e){return s.releasePort(e.debugPort),this._removeWorkerFromList(e),this._ensureMinWorkers(),new o((function(r,t){e.terminate(!1,(function(o){o?t(o):r(e)}))}))},u.prototype._removeWorkerFromList=function(e){var r=this.workers.indexOf(e);-1!==r&&this.workers.splice(r,1)},u.prototype.terminate=function(e,r){this.tasks.forEach((function(e){e.resolver.reject(new Error("Pool terminated"))})),this.tasks.length=0;var t=function(e){this._removeWorkerFromList(e)}.bind(this),n=[];return this.workers.slice().forEach((function(o){var i=o.terminateAndNotify(e,r).then(t);n.push(i)})),o.all(n)},u.prototype.stats=function(){var e=this.workers.length,r=this.workers.filter((function(e){return e.busy()})).length;return{totalWorkers:e,busyWorkers:r,idleWorkers:e-r,pendingTasks:this.tasks.length,activeTasks:r}},u.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var e=this.workers.length;e<this.minWorkers;e++)this.workers.push(this._createWorkerHandler())},u.prototype._createWorkerHandler=function(){return new n(this.script,{forkArgs:this.forkArgs,forkOpts:this.forkOpts,debugPort:s.nextAvailableStartingAt(this.debugPortStart),workerType:this.workerType})},e.exports=u},219:function
|
||
|
//# sourceMappingURL=workerpool.min.js.map
|