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