Comparing version 0.10.1 to 0.11.0
# threads.js - Changelog | ||
## 0.11.0 | ||
- Implement job abortion (#78, credits to @Liu233w) | ||
## 0.10.1 | ||
@@ -4,0 +8,0 @@ |
@@ -484,2 +484,6 @@ require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"./defaults":[function(require,module,exports){ | ||
Job.prototype.abort = function abort() { | ||
this.emit('abort'); | ||
}; | ||
Job.prototype.destroy = function destroy() { | ||
@@ -538,2 +542,3 @@ this.removeAllListeners(); | ||
_this.runArgs = []; | ||
_this.spawnOptions = options; | ||
@@ -576,2 +581,7 @@ _this.on('newJob', function (job) { | ||
Pool.prototype.queueJob = function queueJob(job) { | ||
var _this2 = this; | ||
job.once('abort', function () { | ||
return _this2.dropJob(job); | ||
}); // triggered by job.abort() | ||
this.jobQueue.push(job); | ||
@@ -581,4 +591,11 @@ this.dequeue(); | ||
Pool.prototype.dropJob = function dropJob(job) { | ||
var index = this.jobQueue.indexOf(job); | ||
if (index !== -1) { | ||
this.jobQueue.splice(index, 1); | ||
} | ||
}; | ||
Pool.prototype.dequeue = function dequeue() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -592,2 +609,4 @@ if (this.jobQueue.length === 0 || this.idleThreads.length === 0) { | ||
job.removeAllListeners('abort'); // remove previous listener | ||
job.once('done', function () { | ||
@@ -598,3 +617,3 @@ for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
return _this2.handleJobSuccess.apply(_this2, [thread, job].concat(args)); | ||
return _this3.handleJobSuccess.apply(_this3, [thread, job].concat(args)); | ||
}).once('error', function () { | ||
@@ -605,3 +624,5 @@ for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
return _this2.handleJobError.apply(_this2, [thread, job].concat(args)); | ||
return _this3.handleJobError.apply(_this3, [thread, job].concat(args)); | ||
}).once('abort', function () { | ||
return _this3.handleJobAbort(thread, job); | ||
}); | ||
@@ -613,6 +634,6 @@ | ||
Pool.prototype.handleNewJob = function handleNewJob(job) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
job.once('readyToRun', function () { | ||
return _this3.queueJob(job); | ||
return _this4.queueJob(job); | ||
}); // triggered by job.send() | ||
@@ -636,3 +657,3 @@ }; | ||
Pool.prototype.handleJobDone = function handleJobDone(thread, job) { | ||
var _this4 = this; | ||
var _this5 = this; | ||
@@ -646,3 +667,3 @@ job.destroy(); // to prevent memory leak | ||
setTimeout(function () { | ||
_this4.emit('finished'); | ||
_this5.emit('finished'); | ||
}, 0); | ||
@@ -652,2 +673,12 @@ } | ||
Pool.prototype.handleJobAbort = function handleJobAbort(thread, job) { | ||
thread.kill(); | ||
var index = this.threads.indexOf(thread); | ||
var newThread = (0, _.spawn)(null, [], this.spawnOptions); | ||
this.threads.splice(index, 1, newThread); | ||
this.handleJobDone(newThread, job); | ||
}; | ||
return Pool; | ||
@@ -654,0 +685,0 @@ }(_eventemitter2.default); |
@@ -1,1 +0,1 @@ | ||
require=function e(t,n,r){function o(s,a){if(!n[s]){if(!t[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(i)return i(s,!0);var c=new Error("Cannot find module '"+s+"'");throw c.code="MODULE_NOT_FOUND",c}var f=n[s]={exports:{}};t[s][0].call(f.exports,function(e){var n=t[s][1][e];return o(n||e)},f,f.exports,e,t,n,r)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s<r.length;s++)o(r[s]);return o}({"./defaults":[function(e,t,n){"use strict";n.__esModule=!0,n.default={pool:{size:navigator.hardwareConcurrency||8}}},{}],"./worker":[function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}function a(e,t){return e&&t?"/"===e.charAt(e.length-1)||"/"===t.charAt(0)?e+t:e+"/"+t:e+t}function u(e){var t=(0,v.getConfig)().basepath.web;return t?a(t,e):e}function c(e){return Array.prototype.slice.call(e)}function f(e){if(e.stack)console.error(e.stack);else if(e.message&&e.filename&&e.lineno){var t=e.filename.match(/^data:text\/javascript/)&&e.filename.length>50?e.filename.substr(0,50)+"...":e.filename;console.error(e.message+" @"+t+":"+e.lineno)}else console.error(e)}n.__esModule=!0;var l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},p=e("eventemitter3"),h=r(p),d=e("./slave-code-uri"),y=r(d),v=e("../config");if("object"!==l(window.Worker)&&"function"!=typeof window.Worker)throw new Error("Browser does not support web workers!");var g=function(e){function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];o(this,t);var s=i(this,e.call(this));return s.currentRunnable=null,s.currentImportScripts=[],s.initWorker(),s.worker.addEventListener("message",s.handleMessage.bind(s)),s.worker.addEventListener("error",s.handleError.bind(s)),n&&s.run(n,r),s}return s(t,e),t.prototype.initWorker=function(){try{this.worker=new window.Worker(y.default)}catch(t){var e=(0,v.getConfig)().fallback.slaveScriptUrl;if(!e)throw t;this.worker=new window.Worker(e)}},t.prototype.run=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return this.alreadyInitializedToRun(e,t)?this:("function"==typeof e?this.runMethod(e,t):this.runScripts(e,t),this.currentRunnable=e,this.currentImportScripts=t,this)},t.prototype.runMethod=function(e,t){var n=e.toString(),r=n.substring(n.indexOf("(")+1,n.indexOf(")")).split(","),o=n.substring(n.indexOf("{")+1,n.lastIndexOf("}"));this.worker.postMessage({initByMethod:!0,method:{args:r,body:o},scripts:t.map(u)})},t.prototype.runScripts=function(e,t){if(!e)throw new Error("Must pass a function or a script URL to run().");this.worker.postMessage({initByScripts:!0,scripts:t.concat([e]).map(u)})},t.prototype.send=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return this.worker.postMessage({doRun:!0,param:e},t),this},t.prototype.kill=function(){return this.worker.terminate(),this.emit("exit"),this},t.prototype.promise=function(){var e=this;return new Promise(function(t,n){var r=void 0,o=void 0;r=function(n){e.removeListener("error",o),t(n)},o=function(t){e.removeListener("message",r),n(t)},e.once("message",r).once("error",o)})},t.prototype.alreadyInitializedToRun=function(e,t){var n=this.currentRunnable===e,r=this.currentImportScripts===t||0===t.length&&0===this.currentImportScripts.length;return n&&r},t.prototype.handleMessage=function(e){if(e.data.error)this.handleError(e.data.error);else if(e.data.progress)this.handleProgress(e.data.progress);else{var t=c(e.data.response);this.emit.apply(this,["message"].concat(t)),this.emit.apply(this,["done"].concat(t))}},t.prototype.handleProgress=function(e){this.emit("progress",e)},t.prototype.handleError=function(e){this.listeners("error",!0)||f(e),e.preventDefault&&e.preventDefault(),this.emit("error",e)},t}(h.default);n.default=g},{"../config":2,"./slave-code-uri":6,eventemitter3:8}],1:[function(e,t,n){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o=e("./index"),i=function(e){return e&&e.__esModule?e:{default:e}}(o);"object"===("undefined"==typeof window?"undefined":r(window))&&(window.thread=i.default),"function"==typeof define?define([],function(){return i.default}):"object"===(void 0===t?"undefined":r(t))&&(t.exports=i.default)},{"./index":3}],2:[function(e,t,n){"use strict";function r(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[];Object.keys(t).forEach(function(o){var i=t[o],a=n.concat([o]);if("object"===(void 0===i?"undefined":s(i))){if(void 0!==e[o]&&"object"!==s(e[o]))throw new Error("Expected config property not to be an object: "+a.join("."));r(e[o],i,a)}else{if("object"===s(e[o]))throw new Error("Expected config property to be an object: "+a.join("."));e[o]=i}})}function o(){return u.get()}function i(){return u.set.apply(u,arguments)}n.__esModule=!0;var s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};n.getConfig=o,n.setConfig=i;var a={basepath:{node:"",web:""},fallback:{slaveScriptUrl:""}},u={get:function(){return a},set:function(e){if("object"!==(void 0===e?"undefined":s(e)))throw new Error("Expected config object.");r(a,e)}};n.default=u},{}],3:[function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function o(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return new p.default(e,t,n)}n.__esModule=!0,n.Pool=n.defaults=n.config=void 0,n.spawn=o,e("native-promise-only");var i=e("./config"),s=r(i),a=e("./defaults"),u=r(a),c=e("./pool"),f=r(c),l=e("./worker"),p=r(l);n.config=s.default,n.defaults=u.default,n.Pool=f.default,n.default={config:s.default,defaults:u.default,Pool:f.default,spawn:o,Worker:p.default}},{"./config":2,"./defaults":"./defaults","./pool":5,"./worker":"./worker","native-promise-only":9}],4:[function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}n.__esModule=!0;var s=e("eventemitter3"),a=function(e){return e&&e.__esModule?e:{default:e}}(s),u=function(e){function t(n){r(this,t);var i=o(this,e.call(this));return i.pool=n,i.thread=null,i.runArgs=[],i.sendArgs=[],n.emit("newJob",i),i}return i(t,e),t.prototype.run=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];if(0===t.length)throw new Error("Cannot call .run() without arguments.");return this.runArgs=t,this},t.prototype.send=function(){if(0===this.runArgs.length)throw new Error("Cannot .send() before .run().");for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return this.sendArgs=t,this.emit("readyToRun"),this},t.prototype.executeOn=function(e){var t,n,r=this,o=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return r.emit.apply(r,["progress"].concat(t))},i=function(){for(var t=arguments.length,n=Array(t),i=0;i<t;i++)n[i]=arguments[i];r.emit.apply(r,["done"].concat(n)),e.removeListener("progress",o)},s=function(){for(var t=arguments.length,n=Array(t),i=0;i<t;i++)n[i]=arguments[i];r.emit.apply(r,["error"].concat(n)),e.removeListener("progress",o)};return(t=(n=e.on("progress",o).once("message",i).once("error",s)).run.apply(n,this.runArgs)).send.apply(t,this.sendArgs),this.thread=e,this.emit("threadChanged"),this},t.prototype.promise=function(){var e=this;return new Promise(function(t){e.thread?t(e.thread.promise()):e.once("threadChanged",function(){t(e.thread.promise())})})},t.prototype.destroy=function(){this.removeAllListeners(),delete this.runArgs,delete this.sendArgs},t}(a.default);n.default=u},{eventemitter3:8}],5:[function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}n.__esModule=!0;var a=e("eventemitter3"),u=r(a),c=e("./job"),f=r(c),l=e("./defaults"),p=r(l),h=e("./"),d=function(e){function t(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};o(this,t);var s=i(this,e.call(this));return s.threads=t.spawn(n||p.default.pool.size,r),s.idleThreads=s.threads.slice(),s.jobQueue=[],s.runArgs=[],s.on("newJob",function(e){return s.handleNewJob(e)}),s.on("threadAvailable",function(){return s.dequeue()}),s}return s(t,e),t.prototype.run=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return this.runArgs=t,this},t.prototype.send=function(){var e;if(!this.runArgs)throw new Error("Pool.send() called without prior Pool.run(). You need to define what to run first.");var t=new f.default(this);return(e=t.run.apply(t,this.runArgs)).send.apply(e,arguments)},t.prototype.killAll=function(){this.threads.forEach(function(e){e.kill()})},t.prototype.queueJob=function(e){this.jobQueue.push(e),this.dequeue()},t.prototype.dequeue=function(){var e=this;if(0!==this.jobQueue.length&&0!==this.idleThreads.length){var t=this.jobQueue.shift(),n=this.idleThreads.shift();t.once("done",function(){for(var r=arguments.length,o=Array(r),i=0;i<r;i++)o[i]=arguments[i];return e.handleJobSuccess.apply(e,[n,t].concat(o))}).once("error",function(){for(var r=arguments.length,o=Array(r),i=0;i<r;i++)o[i]=arguments[i];return e.handleJobError.apply(e,[n,t].concat(o))}),t.executeOn(n)}},t.prototype.handleNewJob=function(e){var t=this;e.once("readyToRun",function(){return t.queueJob(e)})},t.prototype.handleJobSuccess=function(e,t){for(var n=arguments.length,r=Array(n>2?n-2:0),o=2;o<n;o++)r[o-2]=arguments[o];this.emit.apply(this,["done",t].concat(r)),this.handleJobDone(e,t)},t.prototype.handleJobError=function(e,t,n){this.emit("error",t,n),this.handleJobDone(e,t)},t.prototype.handleJobDone=function(e,t){var n=this;t.destroy(),this.idleThreads.push(e),this.emit("threadAvailable"),this.idleThreads.length===this.threads.length&&setTimeout(function(){n.emit("finished")},0)},t}(u.default);n.default=d,d.spawn=function(e,t){for(var n=[],r=0;r<e;r++)n.push((0,h.spawn)(null,[],t));return n}},{"./":3,"./defaults":"./defaults","./job":4,eventemitter3:8}],6:[function(e,t,n){"use strict";n.__esModule=!0;var r=e("./slave-code"),o=function(e){return e&&e.__esModule?e:{default:e}}(r),i="data:text/javascript;charset=utf-8,"+encodeURI(o.default),s=window.createBlobURL||window.createObjectURL;if(!s){var a=window.URL||window.webkitURL;if(!a)throw new Error("No Blob creation implementation found.");s=a.createObjectURL}if("function"==typeof window.BlobBuilder&&"function"==typeof s){var u=new window.BlobBuilder;u.append(o.default),i=s(u.getBlob())}else if("function"==typeof window.Blob&&"function"==typeof s){var c=new window.Blob([o.default],{type:"text/javascript"});i=s(c)}n.default=i},{"./slave-code":7}],7:[function(e,t,n){t.exports="/*eslint-env worker*/\n/*global importScripts*/\n/*eslint-disable no-console*/\nself.module = {\n exports : function() {\n if (console) { console.error('No thread logic initialized.'); }\n }\n};\n\nfunction handlerDone() {\n var args = Array.prototype.slice.call(arguments, 0);\n this.postMessage({ response : args });\n}\n\nfunction handlerProgress(progress) {\n this.postMessage({ progress : progress });\n}\n\nfunction handlerError(error) {\n // Need to clone error manually to avoid DataCloneError, since errors cannot be send\n var cloned = {\n message: error.message,\n name: error.name,\n stack: error.stack\n };\n this.postMessage({ error : cloned });\n}\n\nfunction handlerDoneTransfer() {\n var args = Array.prototype.slice.call(arguments);\n var lastArg = args.pop();\n\n if (!(lastArg instanceof Array) && this.console) {\n console.error('Expected 2nd parameter of <doneCallback>.transfer() to be an array. Got:', lastArg);\n }\n\n this.postMessage({ response : args }, lastArg);\n}\n\nfunction isPromise (thing) {\n return thing && typeof thing.then === 'function';\n}\n\nself.onmessage = function (event) {\n var scripts = event.data.scripts;\n if (scripts && scripts.length > 0 && typeof importScripts !== 'function') {\n throw new Error('importScripts() not supported.');\n }\n\n if (event.data.initByScripts) {\n importScripts.apply(null, scripts);\n }\n\n if (event.data.initByMethod) {\n // Clear `this.module.exports` first, to avoid trouble with importScripts' CommonJS detection\n delete this.module.exports;\n \n if (scripts && scripts.length > 0) {\n importScripts.apply(null, scripts);\n }\n\n var method = event.data.method;\n this.module.exports = Function.apply(null, method.args.concat(method.body));\n }\n\n if (event.data.doRun) {\n var handler = this.module.exports;\n\n if (typeof handler !== 'function') {\n throw new Error('Cannot run thread logic. No handler has been exported.');\n }\n\n var preparedHandlerDone = handlerDone.bind(this);\n preparedHandlerDone.transfer = handlerDoneTransfer.bind(this);\n\n var returned = handler.call(this, event.data.param, preparedHandlerDone, handlerProgress.bind(this));\n\n if (isPromise(returned)) {\n returned.then(preparedHandlerDone, handlerError.bind(this));\n }\n }\n}.bind(self);\n"},{}],8:[function(e,t,n){"use strict";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function i(){this._events=new r,this._eventsCount=0}var s=Object.prototype.hasOwnProperty,a="~";Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(a=!1)),i.prototype.eventNames=function(){var e,t,n=[];if(0===this._eventsCount)return n;for(t in e=this._events)s.call(e,t)&&n.push(a?t.slice(1):t);return Object.getOwnPropertySymbols?n.concat(Object.getOwnPropertySymbols(e)):n},i.prototype.listeners=function(e,t){var n=a?a+e:e,r=this._events[n];if(t)return!!r;if(!r)return[];if(r.fn)return[r.fn];for(var o=0,i=r.length,s=new Array(i);o<i;o++)s[o]=r[o].fn;return s},i.prototype.emit=function(e,t,n,r,o,i){var s=a?a+e:e;if(!this._events[s])return!1;var u,c,f=this._events[s],l=arguments.length;if(f.fn){switch(f.once&&this.removeListener(e,f.fn,void 0,!0),l){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,t),!0;case 3:return f.fn.call(f.context,t,n),!0;case 4:return f.fn.call(f.context,t,n,r),!0;case 5:return f.fn.call(f.context,t,n,r,o),!0;case 6:return f.fn.call(f.context,t,n,r,o,i),!0}for(c=1,u=new Array(l-1);c<l;c++)u[c-1]=arguments[c];f.fn.apply(f.context,u)}else{var p,h=f.length;for(c=0;c<h;c++)switch(f[c].once&&this.removeListener(e,f[c].fn,void 0,!0),l){case 1:f[c].fn.call(f[c].context);break;case 2:f[c].fn.call(f[c].context,t);break;case 3:f[c].fn.call(f[c].context,t,n);break;case 4:f[c].fn.call(f[c].context,t,n,r);break;default:if(!u)for(p=1,u=new Array(l-1);p<l;p++)u[p-1]=arguments[p];f[c].fn.apply(f[c].context,u)}}return!0},i.prototype.on=function(e,t,n){var r=new o(t,n||this),i=a?a+e:e;return this._events[i]?this._events[i].fn?this._events[i]=[this._events[i],r]:this._events[i].push(r):(this._events[i]=r,this._eventsCount++),this},i.prototype.once=function(e,t,n){var r=new o(t,n||this,!0),i=a?a+e:e;return this._events[i]?this._events[i].fn?this._events[i]=[this._events[i],r]:this._events[i].push(r):(this._events[i]=r,this._eventsCount++),this},i.prototype.removeListener=function(e,t,n,o){var i=a?a+e:e;if(!this._events[i])return this;if(!t)return 0==--this._eventsCount?this._events=new r:delete this._events[i],this;var s=this._events[i];if(s.fn)s.fn!==t||o&&!s.once||n&&s.context!==n||(0==--this._eventsCount?this._events=new r:delete this._events[i]);else{for(var u=0,c=[],f=s.length;u<f;u++)(s[u].fn!==t||o&&!s[u].once||n&&s[u].context!==n)&&c.push(s[u]);c.length?this._events[i]=1===c.length?c[0]:c:0==--this._eventsCount?this._events=new r:delete this._events[i]}return this},i.prototype.removeAllListeners=function(e){var t;return e?(t=a?a+e:e,this._events[t]&&(0==--this._eventsCount?this._events=new r:delete this._events[t])):(this._events=new r,this._eventsCount=0),this},i.prototype.off=i.prototype.removeListener,i.prototype.addListener=i.prototype.on,i.prototype.setMaxListeners=function(){return this},i.prefixed=a,i.EventEmitter=i,void 0!==t&&(t.exports=i)},{}],9:[function(e,t,n){(function(e){!function(e,n,r){n[e]=n[e]||r(),void 0!==t&&t.exports?t.exports=n[e]:"function"==typeof define&&define.amd&&define(function(){return n[e]})}("Promise",void 0!==e?e:this,function(){"use strict";function e(e,t){p.add(e,t),l||(l=d(p.drain))}function t(e){var t,n=typeof e;return null==e||"object"!=n&&"function"!=n||(t=e.then),"function"==typeof t&&t}function n(){for(var e=0;e<this.chain.length;e++)r(this,1===this.state?this.chain[e].success:this.chain[e].failure,this.chain[e]);this.chain.length=0}function r(e,n,r){var o,i;try{!1===n?r.reject(e.msg):(o=!0===n?e.msg:n.call(void 0,e.msg),o===r.promise?r.reject(TypeError("Promise-chain cycle")):(i=t(o))?i.call(o,r.resolve,r.reject):r.resolve(o))}catch(e){r.reject(e)}}function o(r){var s,u=this;if(!u.triggered){u.triggered=!0,u.def&&(u=u.def);try{(s=t(r))?e(function(){var e=new a(u);try{s.call(r,function(){o.apply(e,arguments)},function(){i.apply(e,arguments)})}catch(t){i.call(e,t)}}):(u.msg=r,u.state=1,u.chain.length>0&&e(n,u))}catch(e){i.call(new a(u),e)}}}function i(t){var r=this;r.triggered||(r.triggered=!0,r.def&&(r=r.def),r.msg=t,r.state=2,r.chain.length>0&&e(n,r))}function s(e,t,n,r){for(var o=0;o<t.length;o++)!function(o){e.resolve(t[o]).then(function(e){n(o,e)},r)}(o)}function a(e){this.def=e,this.triggered=!1}function u(e){this.promise=e,this.state=0,this.triggered=!1,this.chain=[],this.msg=void 0}function c(t){if("function"!=typeof t)throw TypeError("Not a function");if(0!==this.__NPO__)throw TypeError("Not a promise");this.__NPO__=1;var r=new u(this);this.then=function(t,o){var i={success:"function"!=typeof t||t,failure:"function"==typeof o&&o};return i.promise=new this.constructor(function(e,t){if("function"!=typeof e||"function"!=typeof t)throw TypeError("Not a function");i.resolve=e,i.reject=t}),r.chain.push(i),0!==r.state&&e(n,r),i.promise},this.catch=function(e){return this.then(void 0,e)};try{t.call(void 0,function(e){o.call(r,e)},function(e){i.call(r,e)})}catch(e){i.call(r,e)}}var f,l,p,h=Object.prototype.toString,d="undefined"!=typeof setImmediate?function(e){return setImmediate(e)}:setTimeout;try{Object.defineProperty({},"x",{}),f=function(e,t,n,r){return Object.defineProperty(e,t,{value:n,writable:!0,configurable:!1!==r})}}catch(e){f=function(e,t,n){return e[t]=n,e}}p=function(){function e(e,t){this.fn=e,this.self=t,this.next=void 0}var t,n,r;return{add:function(o,i){r=new e(o,i),n?n.next=r:t=r,n=r,r=void 0},drain:function(){var e=t;for(t=n=l=void 0;e;)e.fn.call(e.self),e=e.next}}}();var y=f({},"constructor",c,!1);return c.prototype=y,f(y,"__NPO__",0,!1),f(c,"resolve",function(e){var t=this;return e&&"object"==typeof e&&1===e.__NPO__?e:new t(function(t,n){if("function"!=typeof t||"function"!=typeof n)throw TypeError("Not a function");t(e)})}),f(c,"reject",function(e){return new this(function(t,n){if("function"!=typeof t||"function"!=typeof n)throw TypeError("Not a function");n(e)})}),f(c,"all",function(e){var t=this;return"[object Array]"!=h.call(e)?t.reject(TypeError("Not an array")):0===e.length?t.resolve([]):new t(function(n,r){if("function"!=typeof n||"function"!=typeof r)throw TypeError("Not a function");var o=e.length,i=Array(o),a=0;s(t,e,function(e,t){i[e]=t,++a===o&&n(i)},r)})}),f(c,"race",function(e){var t=this;return"[object Array]"!=h.call(e)?t.reject(TypeError("Not an array")):new t(function(n,r){if("function"!=typeof n||"function"!=typeof r)throw TypeError("Not a function");s(t,e,function(e,t){n(t)},r)})}),c})}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1]); | ||
require=function e(t,n,r){function o(s,a){if(!n[s]){if(!t[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(i)return i(s,!0);var c=new Error("Cannot find module '"+s+"'");throw c.code="MODULE_NOT_FOUND",c}var f=n[s]={exports:{}};t[s][0].call(f.exports,function(e){var n=t[s][1][e];return o(n||e)},f,f.exports,e,t,n,r)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s<r.length;s++)o(r[s]);return o}({"./defaults":[function(e,t,n){"use strict";n.__esModule=!0,n.default={pool:{size:navigator.hardwareConcurrency||8}}},{}],"./worker":[function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}function a(e,t){return e&&t?"/"===e.charAt(e.length-1)||"/"===t.charAt(0)?e+t:e+"/"+t:e+t}function u(e){var t=(0,v.getConfig)().basepath.web;return t?a(t,e):e}function c(e){return Array.prototype.slice.call(e)}function f(e){if(e.stack)console.error(e.stack);else if(e.message&&e.filename&&e.lineno){var t=e.filename.match(/^data:text\/javascript/)&&e.filename.length>50?e.filename.substr(0,50)+"...":e.filename;console.error(e.message+" @"+t+":"+e.lineno)}else console.error(e)}n.__esModule=!0;var l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},p=e("eventemitter3"),h=r(p),d=e("./slave-code-uri"),y=r(d),v=e("../config");if("object"!==l(window.Worker)&&"function"!=typeof window.Worker)throw new Error("Browser does not support web workers!");var g=function(e){function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];o(this,t);var s=i(this,e.call(this));return s.currentRunnable=null,s.currentImportScripts=[],s.initWorker(),s.worker.addEventListener("message",s.handleMessage.bind(s)),s.worker.addEventListener("error",s.handleError.bind(s)),n&&s.run(n,r),s}return s(t,e),t.prototype.initWorker=function(){try{this.worker=new window.Worker(y.default)}catch(t){var e=(0,v.getConfig)().fallback.slaveScriptUrl;if(!e)throw t;this.worker=new window.Worker(e)}},t.prototype.run=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return this.alreadyInitializedToRun(e,t)?this:("function"==typeof e?this.runMethod(e,t):this.runScripts(e,t),this.currentRunnable=e,this.currentImportScripts=t,this)},t.prototype.runMethod=function(e,t){var n=e.toString(),r=n.substring(n.indexOf("(")+1,n.indexOf(")")).split(","),o=n.substring(n.indexOf("{")+1,n.lastIndexOf("}"));this.worker.postMessage({initByMethod:!0,method:{args:r,body:o},scripts:t.map(u)})},t.prototype.runScripts=function(e,t){if(!e)throw new Error("Must pass a function or a script URL to run().");this.worker.postMessage({initByScripts:!0,scripts:t.concat([e]).map(u)})},t.prototype.send=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return this.worker.postMessage({doRun:!0,param:e},t),this},t.prototype.kill=function(){return this.worker.terminate(),this.emit("exit"),this},t.prototype.promise=function(){var e=this;return new Promise(function(t,n){var r=void 0,o=void 0;r=function(n){e.removeListener("error",o),t(n)},o=function(t){e.removeListener("message",r),n(t)},e.once("message",r).once("error",o)})},t.prototype.alreadyInitializedToRun=function(e,t){var n=this.currentRunnable===e,r=this.currentImportScripts===t||0===t.length&&0===this.currentImportScripts.length;return n&&r},t.prototype.handleMessage=function(e){if(e.data.error)this.handleError(e.data.error);else if(e.data.progress)this.handleProgress(e.data.progress);else{var t=c(e.data.response);this.emit.apply(this,["message"].concat(t)),this.emit.apply(this,["done"].concat(t))}},t.prototype.handleProgress=function(e){this.emit("progress",e)},t.prototype.handleError=function(e){this.listeners("error",!0)||f(e),e.preventDefault&&e.preventDefault(),this.emit("error",e)},t}(h.default);n.default=g},{"../config":2,"./slave-code-uri":6,eventemitter3:8}],1:[function(e,t,n){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o=e("./index"),i=function(e){return e&&e.__esModule?e:{default:e}}(o);"object"===("undefined"==typeof window?"undefined":r(window))&&(window.thread=i.default),"function"==typeof define?define([],function(){return i.default}):"object"===(void 0===t?"undefined":r(t))&&(t.exports=i.default)},{"./index":3}],2:[function(e,t,n){"use strict";function r(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[];Object.keys(t).forEach(function(o){var i=t[o],a=n.concat([o]);if("object"===(void 0===i?"undefined":s(i))){if(void 0!==e[o]&&"object"!==s(e[o]))throw new Error("Expected config property not to be an object: "+a.join("."));r(e[o],i,a)}else{if("object"===s(e[o]))throw new Error("Expected config property to be an object: "+a.join("."));e[o]=i}})}function o(){return u.get()}function i(){return u.set.apply(u,arguments)}n.__esModule=!0;var s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};n.getConfig=o,n.setConfig=i;var a={basepath:{node:"",web:""},fallback:{slaveScriptUrl:""}},u={get:function(){return a},set:function(e){if("object"!==(void 0===e?"undefined":s(e)))throw new Error("Expected config object.");r(a,e)}};n.default=u},{}],3:[function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function o(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return new p.default(e,t,n)}n.__esModule=!0,n.Pool=n.defaults=n.config=void 0,n.spawn=o,e("native-promise-only");var i=e("./config"),s=r(i),a=e("./defaults"),u=r(a),c=e("./pool"),f=r(c),l=e("./worker"),p=r(l);n.config=s.default,n.defaults=u.default,n.Pool=f.default,n.default={config:s.default,defaults:u.default,Pool:f.default,spawn:o,Worker:p.default}},{"./config":2,"./defaults":"./defaults","./pool":5,"./worker":"./worker","native-promise-only":9}],4:[function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}n.__esModule=!0;var s=e("eventemitter3"),a=function(e){return e&&e.__esModule?e:{default:e}}(s),u=function(e){function t(n){r(this,t);var i=o(this,e.call(this));return i.pool=n,i.thread=null,i.runArgs=[],i.sendArgs=[],n.emit("newJob",i),i}return i(t,e),t.prototype.run=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];if(0===t.length)throw new Error("Cannot call .run() without arguments.");return this.runArgs=t,this},t.prototype.send=function(){if(0===this.runArgs.length)throw new Error("Cannot .send() before .run().");for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return this.sendArgs=t,this.emit("readyToRun"),this},t.prototype.executeOn=function(e){var t,n,r=this,o=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return r.emit.apply(r,["progress"].concat(t))},i=function(){for(var t=arguments.length,n=Array(t),i=0;i<t;i++)n[i]=arguments[i];r.emit.apply(r,["done"].concat(n)),e.removeListener("progress",o)},s=function(){for(var t=arguments.length,n=Array(t),i=0;i<t;i++)n[i]=arguments[i];r.emit.apply(r,["error"].concat(n)),e.removeListener("progress",o)};return(t=(n=e.on("progress",o).once("message",i).once("error",s)).run.apply(n,this.runArgs)).send.apply(t,this.sendArgs),this.thread=e,this.emit("threadChanged"),this},t.prototype.promise=function(){var e=this;return new Promise(function(t){e.thread?t(e.thread.promise()):e.once("threadChanged",function(){t(e.thread.promise())})})},t.prototype.abort=function(){this.emit("abort")},t.prototype.destroy=function(){this.removeAllListeners(),delete this.runArgs,delete this.sendArgs},t}(a.default);n.default=u},{eventemitter3:8}],5:[function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}n.__esModule=!0;var a=e("eventemitter3"),u=r(a),c=e("./job"),f=r(c),l=e("./defaults"),p=r(l),h=e("./"),d=function(e){function t(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};o(this,t);var s=i(this,e.call(this));return s.threads=t.spawn(n||p.default.pool.size,r),s.idleThreads=s.threads.slice(),s.jobQueue=[],s.runArgs=[],s.spawnOptions=r,s.on("newJob",function(e){return s.handleNewJob(e)}),s.on("threadAvailable",function(){return s.dequeue()}),s}return s(t,e),t.prototype.run=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return this.runArgs=t,this},t.prototype.send=function(){var e;if(!this.runArgs)throw new Error("Pool.send() called without prior Pool.run(). You need to define what to run first.");var t=new f.default(this);return(e=t.run.apply(t,this.runArgs)).send.apply(e,arguments)},t.prototype.killAll=function(){this.threads.forEach(function(e){e.kill()})},t.prototype.queueJob=function(e){var t=this;e.once("abort",function(){return t.dropJob(e)}),this.jobQueue.push(e),this.dequeue()},t.prototype.dropJob=function(e){var t=this.jobQueue.indexOf(e);-1!==t&&this.jobQueue.splice(t,1)},t.prototype.dequeue=function(){var e=this;if(0!==this.jobQueue.length&&0!==this.idleThreads.length){var t=this.jobQueue.shift(),n=this.idleThreads.shift();t.removeAllListeners("abort"),t.once("done",function(){for(var r=arguments.length,o=Array(r),i=0;i<r;i++)o[i]=arguments[i];return e.handleJobSuccess.apply(e,[n,t].concat(o))}).once("error",function(){for(var r=arguments.length,o=Array(r),i=0;i<r;i++)o[i]=arguments[i];return e.handleJobError.apply(e,[n,t].concat(o))}).once("abort",function(){return e.handleJobAbort(n,t)}),t.executeOn(n)}},t.prototype.handleNewJob=function(e){var t=this;e.once("readyToRun",function(){return t.queueJob(e)})},t.prototype.handleJobSuccess=function(e,t){for(var n=arguments.length,r=Array(n>2?n-2:0),o=2;o<n;o++)r[o-2]=arguments[o];this.emit.apply(this,["done",t].concat(r)),this.handleJobDone(e,t)},t.prototype.handleJobError=function(e,t,n){this.emit("error",t,n),this.handleJobDone(e,t)},t.prototype.handleJobDone=function(e,t){var n=this;t.destroy(),this.idleThreads.push(e),this.emit("threadAvailable"),this.idleThreads.length===this.threads.length&&setTimeout(function(){n.emit("finished")},0)},t.prototype.handleJobAbort=function(e,t){e.kill();var n=this.threads.indexOf(e),r=(0,h.spawn)(null,[],this.spawnOptions);this.threads.splice(n,1,r),this.handleJobDone(r,t)},t}(u.default);n.default=d,d.spawn=function(e,t){for(var n=[],r=0;r<e;r++)n.push((0,h.spawn)(null,[],t));return n}},{"./":3,"./defaults":"./defaults","./job":4,eventemitter3:8}],6:[function(e,t,n){"use strict";n.__esModule=!0;var r=e("./slave-code"),o=function(e){return e&&e.__esModule?e:{default:e}}(r),i="data:text/javascript;charset=utf-8,"+encodeURI(o.default),s=window.createBlobURL||window.createObjectURL;if(!s){var a=window.URL||window.webkitURL;if(!a)throw new Error("No Blob creation implementation found.");s=a.createObjectURL}if("function"==typeof window.BlobBuilder&&"function"==typeof s){var u=new window.BlobBuilder;u.append(o.default),i=s(u.getBlob())}else if("function"==typeof window.Blob&&"function"==typeof s){var c=new window.Blob([o.default],{type:"text/javascript"});i=s(c)}n.default=i},{"./slave-code":7}],7:[function(e,t,n){t.exports="/*eslint-env worker*/\n/*global importScripts*/\n/*eslint-disable no-console*/\nself.module = {\n exports : function() {\n if (console) { console.error('No thread logic initialized.'); }\n }\n};\n\nfunction handlerDone() {\n var args = Array.prototype.slice.call(arguments, 0);\n this.postMessage({ response : args });\n}\n\nfunction handlerProgress(progress) {\n this.postMessage({ progress : progress });\n}\n\nfunction handlerError(error) {\n // Need to clone error manually to avoid DataCloneError, since errors cannot be send\n var cloned = {\n message: error.message,\n name: error.name,\n stack: error.stack\n };\n this.postMessage({ error : cloned });\n}\n\nfunction handlerDoneTransfer() {\n var args = Array.prototype.slice.call(arguments);\n var lastArg = args.pop();\n\n if (!(lastArg instanceof Array) && this.console) {\n console.error('Expected 2nd parameter of <doneCallback>.transfer() to be an array. Got:', lastArg);\n }\n\n this.postMessage({ response : args }, lastArg);\n}\n\nfunction isPromise (thing) {\n return thing && typeof thing.then === 'function';\n}\n\nself.onmessage = function (event) {\n var scripts = event.data.scripts;\n if (scripts && scripts.length > 0 && typeof importScripts !== 'function') {\n throw new Error('importScripts() not supported.');\n }\n\n if (event.data.initByScripts) {\n importScripts.apply(null, scripts);\n }\n\n if (event.data.initByMethod) {\n // Clear `this.module.exports` first, to avoid trouble with importScripts' CommonJS detection\n delete this.module.exports;\n \n if (scripts && scripts.length > 0) {\n importScripts.apply(null, scripts);\n }\n\n var method = event.data.method;\n this.module.exports = Function.apply(null, method.args.concat(method.body));\n }\n\n if (event.data.doRun) {\n var handler = this.module.exports;\n\n if (typeof handler !== 'function') {\n throw new Error('Cannot run thread logic. No handler has been exported.');\n }\n\n var preparedHandlerDone = handlerDone.bind(this);\n preparedHandlerDone.transfer = handlerDoneTransfer.bind(this);\n\n var returned = handler.call(this, event.data.param, preparedHandlerDone, handlerProgress.bind(this));\n\n if (isPromise(returned)) {\n returned.then(preparedHandlerDone, handlerError.bind(this));\n }\n }\n}.bind(self);\n"},{}],8:[function(e,t,n){"use strict";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function i(){this._events=new r,this._eventsCount=0}var s=Object.prototype.hasOwnProperty,a="~";Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(a=!1)),i.prototype.eventNames=function(){var e,t,n=[];if(0===this._eventsCount)return n;for(t in e=this._events)s.call(e,t)&&n.push(a?t.slice(1):t);return Object.getOwnPropertySymbols?n.concat(Object.getOwnPropertySymbols(e)):n},i.prototype.listeners=function(e,t){var n=a?a+e:e,r=this._events[n];if(t)return!!r;if(!r)return[];if(r.fn)return[r.fn];for(var o=0,i=r.length,s=new Array(i);o<i;o++)s[o]=r[o].fn;return s},i.prototype.emit=function(e,t,n,r,o,i){var s=a?a+e:e;if(!this._events[s])return!1;var u,c,f=this._events[s],l=arguments.length;if(f.fn){switch(f.once&&this.removeListener(e,f.fn,void 0,!0),l){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,t),!0;case 3:return f.fn.call(f.context,t,n),!0;case 4:return f.fn.call(f.context,t,n,r),!0;case 5:return f.fn.call(f.context,t,n,r,o),!0;case 6:return f.fn.call(f.context,t,n,r,o,i),!0}for(c=1,u=new Array(l-1);c<l;c++)u[c-1]=arguments[c];f.fn.apply(f.context,u)}else{var p,h=f.length;for(c=0;c<h;c++)switch(f[c].once&&this.removeListener(e,f[c].fn,void 0,!0),l){case 1:f[c].fn.call(f[c].context);break;case 2:f[c].fn.call(f[c].context,t);break;case 3:f[c].fn.call(f[c].context,t,n);break;case 4:f[c].fn.call(f[c].context,t,n,r);break;default:if(!u)for(p=1,u=new Array(l-1);p<l;p++)u[p-1]=arguments[p];f[c].fn.apply(f[c].context,u)}}return!0},i.prototype.on=function(e,t,n){var r=new o(t,n||this),i=a?a+e:e;return this._events[i]?this._events[i].fn?this._events[i]=[this._events[i],r]:this._events[i].push(r):(this._events[i]=r,this._eventsCount++),this},i.prototype.once=function(e,t,n){var r=new o(t,n||this,!0),i=a?a+e:e;return this._events[i]?this._events[i].fn?this._events[i]=[this._events[i],r]:this._events[i].push(r):(this._events[i]=r,this._eventsCount++),this},i.prototype.removeListener=function(e,t,n,o){var i=a?a+e:e;if(!this._events[i])return this;if(!t)return 0==--this._eventsCount?this._events=new r:delete this._events[i],this;var s=this._events[i];if(s.fn)s.fn!==t||o&&!s.once||n&&s.context!==n||(0==--this._eventsCount?this._events=new r:delete this._events[i]);else{for(var u=0,c=[],f=s.length;u<f;u++)(s[u].fn!==t||o&&!s[u].once||n&&s[u].context!==n)&&c.push(s[u]);c.length?this._events[i]=1===c.length?c[0]:c:0==--this._eventsCount?this._events=new r:delete this._events[i]}return this},i.prototype.removeAllListeners=function(e){var t;return e?(t=a?a+e:e,this._events[t]&&(0==--this._eventsCount?this._events=new r:delete this._events[t])):(this._events=new r,this._eventsCount=0),this},i.prototype.off=i.prototype.removeListener,i.prototype.addListener=i.prototype.on,i.prototype.setMaxListeners=function(){return this},i.prefixed=a,i.EventEmitter=i,void 0!==t&&(t.exports=i)},{}],9:[function(e,t,n){(function(e){!function(e,n,r){n[e]=n[e]||r(),void 0!==t&&t.exports?t.exports=n[e]:"function"==typeof define&&define.amd&&define(function(){return n[e]})}("Promise",void 0!==e?e:this,function(){"use strict";function e(e,t){p.add(e,t),l||(l=d(p.drain))}function t(e){var t,n=typeof e;return null==e||"object"!=n&&"function"!=n||(t=e.then),"function"==typeof t&&t}function n(){for(var e=0;e<this.chain.length;e++)r(this,1===this.state?this.chain[e].success:this.chain[e].failure,this.chain[e]);this.chain.length=0}function r(e,n,r){var o,i;try{!1===n?r.reject(e.msg):(o=!0===n?e.msg:n.call(void 0,e.msg),o===r.promise?r.reject(TypeError("Promise-chain cycle")):(i=t(o))?i.call(o,r.resolve,r.reject):r.resolve(o))}catch(e){r.reject(e)}}function o(r){var s,u=this;if(!u.triggered){u.triggered=!0,u.def&&(u=u.def);try{(s=t(r))?e(function(){var e=new a(u);try{s.call(r,function(){o.apply(e,arguments)},function(){i.apply(e,arguments)})}catch(t){i.call(e,t)}}):(u.msg=r,u.state=1,u.chain.length>0&&e(n,u))}catch(e){i.call(new a(u),e)}}}function i(t){var r=this;r.triggered||(r.triggered=!0,r.def&&(r=r.def),r.msg=t,r.state=2,r.chain.length>0&&e(n,r))}function s(e,t,n,r){for(var o=0;o<t.length;o++)!function(o){e.resolve(t[o]).then(function(e){n(o,e)},r)}(o)}function a(e){this.def=e,this.triggered=!1}function u(e){this.promise=e,this.state=0,this.triggered=!1,this.chain=[],this.msg=void 0}function c(t){if("function"!=typeof t)throw TypeError("Not a function");if(0!==this.__NPO__)throw TypeError("Not a promise");this.__NPO__=1;var r=new u(this);this.then=function(t,o){var i={success:"function"!=typeof t||t,failure:"function"==typeof o&&o};return i.promise=new this.constructor(function(e,t){if("function"!=typeof e||"function"!=typeof t)throw TypeError("Not a function");i.resolve=e,i.reject=t}),r.chain.push(i),0!==r.state&&e(n,r),i.promise},this.catch=function(e){return this.then(void 0,e)};try{t.call(void 0,function(e){o.call(r,e)},function(e){i.call(r,e)})}catch(e){i.call(r,e)}}var f,l,p,h=Object.prototype.toString,d="undefined"!=typeof setImmediate?function(e){return setImmediate(e)}:setTimeout;try{Object.defineProperty({},"x",{}),f=function(e,t,n,r){return Object.defineProperty(e,t,{value:n,writable:!0,configurable:!1!==r})}}catch(e){f=function(e,t,n){return e[t]=n,e}}p=function(){function e(e,t){this.fn=e,this.self=t,this.next=void 0}var t,n,r;return{add:function(o,i){r=new e(o,i),n?n.next=r:t=r,n=r,r=void 0},drain:function(){var e=t;for(t=n=l=void 0;e;)e.fn.call(e.self),e=e.next}}}();var y=f({},"constructor",c,!1);return c.prototype=y,f(y,"__NPO__",0,!1),f(c,"resolve",function(e){var t=this;return e&&"object"==typeof e&&1===e.__NPO__?e:new t(function(t,n){if("function"!=typeof t||"function"!=typeof n)throw TypeError("Not a function");t(e)})}),f(c,"reject",function(e){return new this(function(t,n){if("function"!=typeof t||"function"!=typeof n)throw TypeError("Not a function");n(e)})}),f(c,"all",function(e){var t=this;return"[object Array]"!=h.call(e)?t.reject(TypeError("Not an array")):0===e.length?t.resolve([]):new t(function(n,r){if("function"!=typeof n||"function"!=typeof r)throw TypeError("Not a function");var o=e.length,i=Array(o),a=0;s(t,e,function(e,t){i[e]=t,++a===o&&n(i)},r)})}),f(c,"race",function(e){var t=this;return"[object Array]"!=h.call(e)?t.reject(TypeError("Not an array")):new t(function(n,r){if("function"!=typeof n||"function"!=typeof r)throw TypeError("Not a function");s(t,e,function(e,t){n(t)},r)})}),c})}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1]); |
@@ -115,2 +115,6 @@ 'use strict'; | ||
Job.prototype.abort = function abort() { | ||
this.emit('abort'); | ||
}; | ||
Job.prototype.destroy = function destroy() { | ||
@@ -117,0 +121,0 @@ this.removeAllListeners(); |
@@ -41,2 +41,3 @@ 'use strict'; | ||
_this.runArgs = []; | ||
_this.spawnOptions = options; | ||
@@ -79,2 +80,7 @@ _this.on('newJob', function (job) { | ||
Pool.prototype.queueJob = function queueJob(job) { | ||
var _this2 = this; | ||
job.once('abort', function () { | ||
return _this2.dropJob(job); | ||
}); // triggered by job.abort() | ||
this.jobQueue.push(job); | ||
@@ -84,4 +90,11 @@ this.dequeue(); | ||
Pool.prototype.dropJob = function dropJob(job) { | ||
var index = this.jobQueue.indexOf(job); | ||
if (index !== -1) { | ||
this.jobQueue.splice(index, 1); | ||
} | ||
}; | ||
Pool.prototype.dequeue = function dequeue() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -95,2 +108,4 @@ if (this.jobQueue.length === 0 || this.idleThreads.length === 0) { | ||
job.removeAllListeners('abort'); // remove previous listener | ||
job.once('done', function () { | ||
@@ -101,3 +116,3 @@ for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
return _this2.handleJobSuccess.apply(_this2, [thread, job].concat(args)); | ||
return _this3.handleJobSuccess.apply(_this3, [thread, job].concat(args)); | ||
}).once('error', function () { | ||
@@ -108,3 +123,5 @@ for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
return _this2.handleJobError.apply(_this2, [thread, job].concat(args)); | ||
return _this3.handleJobError.apply(_this3, [thread, job].concat(args)); | ||
}).once('abort', function () { | ||
return _this3.handleJobAbort(thread, job); | ||
}); | ||
@@ -116,6 +133,6 @@ | ||
Pool.prototype.handleNewJob = function handleNewJob(job) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
job.once('readyToRun', function () { | ||
return _this3.queueJob(job); | ||
return _this4.queueJob(job); | ||
}); // triggered by job.send() | ||
@@ -139,3 +156,3 @@ }; | ||
Pool.prototype.handleJobDone = function handleJobDone(thread, job) { | ||
var _this4 = this; | ||
var _this5 = this; | ||
@@ -149,3 +166,3 @@ job.destroy(); // to prevent memory leak | ||
setTimeout(function () { | ||
_this4.emit('finished'); | ||
_this5.emit('finished'); | ||
}, 0); | ||
@@ -155,2 +172,12 @@ } | ||
Pool.prototype.handleJobAbort = function handleJobAbort(thread, job) { | ||
thread.kill(); | ||
var index = this.threads.indexOf(thread); | ||
var newThread = (0, _.spawn)(null, [], this.spawnOptions); | ||
this.threads.splice(index, 1, newThread); | ||
this.handleJobDone(newThread, job); | ||
}; | ||
return Pool; | ||
@@ -157,0 +184,0 @@ }(_eventemitter2.default); |
{ | ||
"name": "threads", | ||
"version": "0.10.1", | ||
"version": "0.11.0", | ||
"keywords": [ | ||
@@ -48,3 +48,3 @@ "threads", | ||
"expect.js": "^0.3.1", | ||
"gulp": "^3.8.11", | ||
"gulp": "^3.9.1", | ||
"gulp-babel": "^6.1.2", | ||
@@ -51,0 +51,0 @@ "gulp-concat": "^2.5.2", |
@@ -186,2 +186,21 @@ # threads.js | ||
#### Job Abortion | ||
You can abort a job by calling `job.abort()`. | ||
```javascript | ||
const Pool = require('threads').Pool; | ||
const pool = new Pool(); | ||
const job = pool | ||
.run('/path/to/worker') | ||
.send({ do : 'something' }); | ||
job.on('abort', () => { console.log('Job Aborted'); }); | ||
// somewhere else | ||
job.abort(); | ||
``` | ||
### Streaming | ||
@@ -188,0 +207,0 @@ |
@@ -46,3 +46,3 @@ | ||
}; | ||
thread | ||
@@ -74,2 +74,6 @@ .on('progress', onProgress) | ||
abort() { | ||
this.emit('abort'); | ||
} | ||
destroy () { | ||
@@ -76,0 +80,0 @@ this.removeAllListeners(); |
@@ -13,2 +13,3 @@ import EventEmitter from 'eventemitter3'; | ||
this.runArgs = []; | ||
this.spawnOptions = options; | ||
@@ -40,2 +41,3 @@ this.on('newJob', (job) => this.handleNewJob(job)); | ||
queueJob(job) { | ||
job.once('abort', () => this.dropJob(job)); // triggered by job.abort() | ||
this.jobQueue.push(job); | ||
@@ -45,2 +47,9 @@ this.dequeue(); | ||
dropJob(job) { | ||
const index = this.jobQueue.indexOf(job); | ||
if (index !== -1) { | ||
this.jobQueue.splice(index, 1); | ||
} | ||
} | ||
dequeue() { | ||
@@ -54,5 +63,8 @@ if (this.jobQueue.length === 0 || this.idleThreads.length === 0) { | ||
job.removeAllListeners('abort'); // remove previous listener | ||
job | ||
.once('done', (...args) => this.handleJobSuccess(thread, job, ...args)) | ||
.once('error', (...args) => this.handleJobError(thread, job, ...args)); | ||
.once('error', (...args) => this.handleJobError(thread, job, ...args)) | ||
.once('abort', () => this.handleJobAbort(thread, job)); | ||
@@ -86,2 +98,12 @@ job.executeOn(thread); | ||
} | ||
handleJobAbort(thread, job) { | ||
thread.kill(); | ||
const index = this.threads.indexOf(thread); | ||
const newThread = spawn(null, [], this.spawnOptions); | ||
this.threads.splice(index, 1, newThread); | ||
this.handleJobDone(newThread, job); | ||
} | ||
} | ||
@@ -88,0 +110,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
211691
2885
417