Comparing version 0.6.1 to 0.7.0
@@ -1,1 +0,1 @@ | ||
function handlerDone(){var r=Array.prototype.slice.call(arguments,0);this.postMessage({response:r})}function handlerProgress(r){this.postMessage({progress:r})}function handlerDoneTransfer(){var r=Array.prototype.slice.call(arguments),e=r.pop();e instanceof Array||!this.console||console.error("Expected 2nd parameter of <doneCallback>.transfer() to be an array. Got:",e),this.postMessage({response:r},e)}self.module={exports:function(){console&&console.error("No thread logic initialized.")}},self.onmessage=function(r){var e=r.data.scripts;if(e&&e.length>0&&"function"!=typeof importScripts)throw new Error("importScripts() not supported.");if(r.data.initByScripts&&importScripts.apply(null,e),r.data.initByMethod){var t=r.data.method;this.module.exports=Function.apply(null,t.args.concat(t.body)),e&&e.length>0&&importScripts.apply(null,e)}if(r.data.doRun){var o=this.module.exports;if("function"!=typeof o)throw new Error("Cannot run thread logic. No handler has been exported.");var n=handlerDone.bind(this);n.transfer=handlerDoneTransfer.bind(this),o.call(this,r.data.param,n,handlerProgress.bind(this))}}.bind(self); | ||
function handlerDone(){var r=Array.prototype.slice.call(arguments,0);this.postMessage({response:r})}function handlerProgress(r){this.postMessage({progress:r})}function handlerDoneTransfer(){var r=Array.prototype.slice.call(arguments),e=r.pop();e instanceof Array||!this.console||console.error("Expected 2nd parameter of <doneCallback>.transfer() to be an array. Got:",e),this.postMessage({response:r},e)}self.module={exports:function(){console&&console.error("No thread logic initialized.")}},self.onmessage=function(r){var e=r.data.scripts;if(e&&e.length>0&&"function"!=typeof importScripts)throw new Error("importScripts() not supported.");if(r.data.initByScripts&&importScripts.apply(null,e),r.data.initByMethod){var o=r.data.method;this.module.exports=Function.apply(null,o.args.concat(o.body)),e&&e.length>0&&importScripts.apply(null,e)}if(r.data.doRun){var t=this.module.exports;if("function"!=typeof t)throw new Error("Cannot run thread logic. No handler has been exported.");var n=handlerDone.bind(this);n.transfer=handlerDoneTransfer.bind(this),t.call(this,r.data.param,n,handlerProgress.bind(this))}}.bind(self); |
@@ -65,5 +65,15 @@ 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){ | ||
function joinPaths(path1, path2) { | ||
if (!path1 || !path2) { | ||
return path1 + path2; | ||
} else if (path1.charAt(path1.length - 1) === '/' || path2.charAt(0) === '/') { | ||
return path1 + path2; | ||
} else { | ||
return path1 + '/' + path2; | ||
} | ||
} | ||
function prependScriptUrl(scriptUrl) { | ||
var prefix = _config.getConfig().basepath.web; | ||
return prefix ? prefix + '/' + scriptUrl : scriptUrl; | ||
return prefix ? joinPaths(prefix, scriptUrl) : scriptUrl; | ||
} | ||
@@ -105,2 +115,6 @@ | ||
// used by `run()` to decide if the worker must be re-initialized | ||
this.currentRunnable = null; | ||
this.currentImportScripts = []; | ||
this.initWorker(); | ||
@@ -133,2 +147,7 @@ this.worker.addEventListener('message', this.handleMessage.bind(this)); | ||
if (this.alreadyInitializedToRun(toRun, importScripts)) { | ||
// don't re-initialize with the new logic if it already has been | ||
return this; | ||
} | ||
if (typeof toRun === 'function') { | ||
@@ -139,2 +158,6 @@ this.runMethod(toRun, importScripts); | ||
} | ||
this.currentRunnable = toRun; | ||
this.currentImportScripts = importScripts; | ||
return this; | ||
@@ -191,2 +214,9 @@ }; | ||
Worker.prototype.alreadyInitializedToRun = function alreadyInitializedToRun(toRun, importScripts) { | ||
var runnablesMatch = this.currentRunnable === toRun; | ||
var importScriptsMatch = this.currentImportScripts === importScripts || importScripts.length === 0 && this.currentImportScripts.length === 0; | ||
return runnablesMatch && importScriptsMatch; | ||
}; | ||
Worker.prototype.handleMessage = function handleMessage(event) { | ||
@@ -457,2 +487,4 @@ if (event.data.error) { | ||
function Pool(threads) { | ||
var _this = this; | ||
_classCallCheck(this, Pool); | ||
@@ -466,3 +498,8 @@ | ||
this.on('newJob', this.handleNewJob.bind(this)); | ||
this.on('newJob', function (job) { | ||
return _this.handleNewJob(job); | ||
}); | ||
this.on('threadAvailable', function () { | ||
return _this.dequeue(); | ||
}); | ||
} | ||
@@ -497,4 +534,6 @@ | ||
Pool.prototype.dequeue = function dequeue() { | ||
var _this2 = this; | ||
if (this.jobQueue.length === 0 || this.idleThreads.length === 0) { | ||
return this.once('threadAvailable', this.dequeue); | ||
return; | ||
} | ||
@@ -505,4 +544,16 @@ | ||
job.once('done', this.handleJobSuccess.bind(this, thread, job)).once('error', this.handleJobError.bind(this, thread, job)); | ||
job.once('done', function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return _this2.handleJobSuccess.apply(_this2, [thread, job].concat(args)); | ||
}).once('error', function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return _this2.handleJobError.apply(_this2, [thread, job].concat(args)); | ||
}); | ||
job.executeOn(thread); | ||
@@ -512,9 +563,13 @@ }; | ||
Pool.prototype.handleNewJob = function handleNewJob(job) { | ||
var _this3 = this; | ||
this.lastCreatedJob = job; | ||
job.once('readyToRun', this.queueJob.bind(this, job)); // triggered by job.send() | ||
job.once('readyToRun', function () { | ||
return _this3.queueJob(job); | ||
}); // triggered by job.send() | ||
}; | ||
Pool.prototype.handleJobSuccess = function handleJobSuccess(thread, job) { | ||
for (var _len = arguments.length, responseArgs = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
responseArgs[_key - 2] = arguments[_key]; | ||
for (var _len3 = arguments.length, responseArgs = Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) { | ||
responseArgs[_key3 - 2] = arguments[_key3]; | ||
} | ||
@@ -532,3 +587,3 @@ | ||
Pool.prototype.handleJobDone = function handleJobDone(thread) { | ||
var _this = this; | ||
var _this4 = this; | ||
@@ -541,3 +596,3 @@ this.idleThreads.push(thread); | ||
setTimeout(function () { | ||
_this.emit('finished'); | ||
_this4.emit('finished'); | ||
}, 0); | ||
@@ -606,2 +661,4 @@ } | ||
var has = Object.prototype.hasOwnProperty; | ||
// | ||
@@ -622,3 +679,3 @@ // We store our EE objects in a plain object whose properties are event names. | ||
* @param {Mixed} context Context for function execution. | ||
* @param {Boolean} once Only emit once | ||
* @param {Boolean} [once=false] Only emit once | ||
* @api private | ||
@@ -642,3 +699,3 @@ */ | ||
/** | ||
* Holds the assigned EventEmitters by name. | ||
* Hold the assigned EventEmitters by name. | ||
* | ||
@@ -651,2 +708,27 @@ * @type {Object} | ||
/** | ||
* Return an array listing the events for which the emitter has registered | ||
* listeners. | ||
* | ||
* @returns {Array} | ||
* @api public | ||
*/ | ||
EventEmitter.prototype.eventNames = function eventNames() { | ||
var events = this._events | ||
, names = [] | ||
, name; | ||
if (!events) return names; | ||
for (name in events) { | ||
if (has.call(events, name)) names.push(prefix ? name.slice(1) : name); | ||
} | ||
if (Object.getOwnPropertySymbols) { | ||
return names.concat(Object.getOwnPropertySymbols(events)); | ||
} | ||
return names; | ||
}; | ||
/** | ||
* Return a list of assigned event listeners. | ||
@@ -736,4 +818,4 @@ * | ||
* @param {String} event Name of the event. | ||
* @param {Functon} fn Callback function. | ||
* @param {Mixed} context The context of the function. | ||
* @param {Function} fn Callback function. | ||
* @param {Mixed} [context=this] The context of the function. | ||
* @api public | ||
@@ -762,3 +844,3 @@ */ | ||
* @param {Function} fn Callback function. | ||
* @param {Mixed} context The context of the function. | ||
* @param {Mixed} [context=this] The context of the function. | ||
* @api public | ||
@@ -765,0 +847,0 @@ */ |
@@ -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 f=new Error("Cannot find module '"+s+"'");throw f.code="MODULE_NOT_FOUND",f}var c=n[s]={exports:{}};t[s][0].call(c.exports,function(e){var n=t[s][1][e];return o(n?n:e)},c,c.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}},t.exports=n["default"]},{}],1:[function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}var o=e("./index"),i=r(o);"object"==typeof window&&(window.thread=i["default"]),"function"==typeof define?define([],function(){return i["default"]}):"object"==typeof t&&(t.exports=i["default"])},{"./index":3}],"./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("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 s(e){var t=p.getConfig().basepath.web;return t?t+"/"+e:e}function a(e){for(var t=[],n=0;"undefined"!=typeof e[n];)t.push(e[n]),n++;return t}function u(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 f=e("eventemitter3"),c=r(f),l=e("./slave-code-uri"),h=r(l),p=e("../config");if("object"!=typeof window.Worker&&"function"!=typeof window.Worker)throw new Error("Browser does not support web workers!");var d=function(e){function t(){var n=arguments.length<=0||void 0===arguments[0]?null:arguments[0],r=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];o(this,t),e.call(this),this.initWorker(),this.worker.addEventListener("message",this.handleMessage.bind(this)),this.worker.addEventListener("error",this.handleError.bind(this)),n&&this.run(n,r)}return i(t,e),t.prototype.initWorker=function(){try{this.worker=new window.Worker(h["default"])}catch(e){var t=p.getConfig().fallback.slaveScriptUrl;if(!t)throw e;this.worker=new window.Worker(h["default"])}},t.prototype.run=function(e){var t=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];return"function"==typeof e?this.runMethod(e,t):this.runScripts(e,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(s)})},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(s)})},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){e.once("message",t).once("error",n)})},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=a(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)||u(e),e.preventDefault&&e.preventDefault(),this.emit("error",e)},t}(c["default"]);n["default"]=d,t.exports=n["default"]},{"../config":2,"./slave-code-uri":6,eventemitter3:8}],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],s=n.concat([o]);if("object"==typeof i){if("undefined"!=typeof e[o]&&"object"!=typeof e[o])throw new Error("Expected config property not to be an object: "+s.join("."));r(e[o],i,s)}else{if("object"==typeof e[o])throw new Error("Expected config property to be an object: "+s.join("."));e[o]=i}})}function o(){return a.get()}function i(){return a.set.apply(a,arguments)}n.__esModule=!0,n.getConfig=o,n.setConfig=i;var s={basepath:{node:"",web:""},fallback:{slaveScriptUrl:""}},a={get:function(){return s},set:function(e){if("object"!=typeof e)throw new Error("Expected config object.");r(s,e)}};n["default"]=a},{}],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]?null:arguments[0],t=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];return new h["default"](e,t)}n.__esModule=!0,n.spawn=o,e("native-promise-only");var i=e("./config"),s=r(i),a=e("./defaults"),u=r(a),f=e("./pool"),c=r(f),l=e("./worker"),h=r(l);n.config=s["default"],n.defaults=u["default"],n.Pool=c["default"],n["default"]={config:s["default"],defaults:u["default"],Pool:c["default"],spawn:o,Worker:h["default"]}},{"./config":2,"./defaults":"./defaults","./pool":5,"./worker":"./worker","native-promise-only":9}],4:[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("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=r(s),u=function(e){function t(n){o(this,t),e.call(this),this.pool=n,this.thread=null,this.runArgs=[],this.sendArgs=[],n.emit("newJob",this)}return i(t,e),t.prototype.run=function(){for(var e=arguments.length,t=Array(e),n=0;e>n;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;e>n;n++)t[n]=arguments[n];return this.sendArgs=t,this.emit("readyToRun"),this},t.prototype.executeOn=function(e){var t,n;return(t=(n=e.once("message",this.emit.bind(this,"done")).once("error",this.emit.bind(this,"error"))).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}(a["default"]);n["default"]=u,t.exports=n["default"]},{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("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=r(s),u=e("./job"),f=r(u),c=e("./defaults"),l=r(c),h=e("./"),p=function(e){function t(n){o(this,t),e.call(this),this.threads=t.spawn(n||l["default"].pool.size),this.idleThreads=this.threads.slice(),this.jobQueue=[],this.runArgs=[],this.on("newJob",this.handleNewJob.bind(this))}return i(t,e),t.prototype.run=function(e){return this.runArgs=e,this},t.prototype.send=function(){if(!this.runArgs)throw new Error("Pool.send() called without prior Pool.run(). You need to define what to run first.");var e=new f["default"](this);return e.run(this.runArgs),e.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(){if(0===this.jobQueue.length||0===this.idleThreads.length)return this.once("threadAvailable",this.dequeue);var e=this.jobQueue.shift(),t=this.idleThreads.shift();e.once("done",this.handleJobSuccess.bind(this,t,e)).once("error",this.handleJobError.bind(this,t,e)),e.executeOn(t)},t.prototype.handleNewJob=function(e){this.lastCreatedJob=e,e.once("readyToRun",this.queueJob.bind(this,e))},t.prototype.handleJobSuccess=function(e,t){for(var n=arguments.length,r=Array(n>2?n-2:0),o=2;n>o;o++)r[o-2]=arguments[o];this.emit.apply(this,["done",t].concat(r)),this.handleJobDone(e)},t.prototype.handleJobError=function(e,t,n){this.emit("error",t,n),this.handleJobDone(e)},t.prototype.handleJobDone=function(e){var t=this;this.idleThreads.push(e),this.emit("threadAvailable"),this.idleThreads.length===this.threads.length&&setTimeout(function(){t.emit("finished")},0)},t}(a["default"]);n["default"]=p,p.spawn=function(e){for(var t=[],n=0;e>n;n++)t.push(h.spawn());return t},t.exports=n["default"]},{"./":3,"./defaults":"./defaults","./job":4,eventemitter3:8}],6:[function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}n.__esModule=!0;var o=e("./slave-code"),i=r(o),s="data:text/javascript;charset=utf-8,"+encodeURI(i["default"]),a=window.createBlobURL||window.createObjectURL;if(!a){var u=window.URL||window.webkitURL;if(!u)throw new Error("No Blob creation implementation found.");a=u.createObjectURL}if("function"==typeof window.BlobBuilder&&"function"==typeof a){var f=new window.BlobBuilder;f.append(i["default"]),s=a(f.getBlob())}else if("function"==typeof window.Blob&&"function"==typeof a){var c=new window.Blob([i["default"]],{type:"text/javascript"});s=a(c)}n["default"]=s,t.exports=n["default"]},{"./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 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\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 var method = event.data.method;\n this.module.exports = Function.apply(null, method.args.concat(method.body));\n\n if (scripts && scripts.length > 0) {\n importScripts.apply(null, scripts);\n }\n }\n\n if (event.data.doRun) {\n var handler = this.module.exports;\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 handler.call(this, event.data.param, preparedHandlerDone, handlerProgress.bind(this));\n }\n}.bind(self);\n"},{}],8:[function(e,t,n){"use strict";function r(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function o(){}var i="function"!=typeof Object.create?"~":!1;o.prototype._events=void 0,o.prototype.listeners=function(e,t){var n=i?i+e:e,r=this._events&&this._events[n];if(t)return!!r;if(!r)return[];if(r.fn)return[r.fn];for(var o=0,s=r.length,a=new Array(s);s>o;o++)a[o]=r[o].fn;return a},o.prototype.emit=function(e,t,n,r,o,s){var a=i?i+e:e;if(!this._events||!this._events[a])return!1;var u,f,c=this._events[a],l=arguments.length;if("function"==typeof c.fn){switch(c.once&&this.removeListener(e,c.fn,void 0,!0),l){case 1:return c.fn.call(c.context),!0;case 2:return c.fn.call(c.context,t),!0;case 3:return c.fn.call(c.context,t,n),!0;case 4:return c.fn.call(c.context,t,n,r),!0;case 5:return c.fn.call(c.context,t,n,r,o),!0;case 6:return c.fn.call(c.context,t,n,r,o,s),!0}for(f=1,u=new Array(l-1);l>f;f++)u[f-1]=arguments[f];c.fn.apply(c.context,u)}else{var h,p=c.length;for(f=0;p>f;f++)switch(c[f].once&&this.removeListener(e,c[f].fn,void 0,!0),l){case 1:c[f].fn.call(c[f].context);break;case 2:c[f].fn.call(c[f].context,t);break;case 3:c[f].fn.call(c[f].context,t,n);break;default:if(!u)for(h=1,u=new Array(l-1);l>h;h++)u[h-1]=arguments[h];c[f].fn.apply(c[f].context,u)}}return!0},o.prototype.on=function(e,t,n){var o=new r(t,n||this),s=i?i+e:e;return this._events||(this._events=i?{}:Object.create(null)),this._events[s]?this._events[s].fn?this._events[s]=[this._events[s],o]:this._events[s].push(o):this._events[s]=o,this},o.prototype.once=function(e,t,n){var o=new r(t,n||this,!0),s=i?i+e:e;return this._events||(this._events=i?{}:Object.create(null)),this._events[s]?this._events[s].fn?this._events[s]=[this._events[s],o]:this._events[s].push(o):this._events[s]=o,this},o.prototype.removeListener=function(e,t,n,r){var o=i?i+e:e;if(!this._events||!this._events[o])return this;var s=this._events[o],a=[];if(t)if(s.fn)(s.fn!==t||r&&!s.once||n&&s.context!==n)&&a.push(s);else for(var u=0,f=s.length;f>u;u++)(s[u].fn!==t||r&&!s[u].once||n&&s[u].context!==n)&&a.push(s[u]);return a.length?this._events[o]=1===a.length?a[0]:a:delete this._events[o],this},o.prototype.removeAllListeners=function(e){return this._events?(e?delete this._events[i?i+e:e]:this._events=i?{}:Object.create(null),this):this},o.prototype.off=o.prototype.removeListener,o.prototype.addListener=o.prototype.on,o.prototype.setMaxListeners=function(){return this},o.prefixed=i,"undefined"!=typeof t&&(t.exports=o)},{}],9:[function(e,t,n){(function(e){!function(e,n,r){n[e]=n[e]||r(),"undefined"!=typeof t&&t.exports?t.exports=n[e]:"function"==typeof define&&define.amd&&define(function(){return n[e]})}("Promise","undefined"!=typeof e?e:this,function(){"use strict";function e(e,t){h.add(e,t),l||(l=d(h.drain))}function t(e){var t,n=typeof e;return null==e||"object"!=n&&"function"!=n||(t=e.then),"function"==typeof t?t:!1}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{n===!1?r.reject(e.msg):(o=n===!0?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(s){r.reject(s)}}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(f){i.call(new a(u),f)}}}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 f(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:!0,failure:"function"==typeof o?o:!1};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(s){i.call(r,s)}}var c,l,h,p=Object.prototype.toString,d="undefined"!=typeof setImmediate?function(e){return setImmediate(e)}:setTimeout;try{Object.defineProperty({},"x",{}),c=function(e,t,n,r){return Object.defineProperty(e,t,{value:n,writable:!0,configurable:r!==!1})}}catch(y){c=function(e,t,n){return e[t]=n,e}}h=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 g=c({},"constructor",f,!1);return f.prototype=g,c(g,"__NPO__",0,!1),c(f,"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)})}),c(f,"reject",function(e){return new this(function(t,n){if("function"!=typeof t||"function"!=typeof n)throw TypeError("Not a function");n(e)})}),c(f,"all",function(e){var t=this;return"[object Array]"!=p.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)})}),c(f,"race",function(e){var t=this;return"[object Array]"!=p.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)})}),f})}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1]); | ||
require=function t(e,n,r){function o(s,a){if(!n[s]){if(!e[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:{}};e[s][0].call(f.exports,function(t){var n=e[s][1][t];return o(n?n:t)},f,f.exports,t,e,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(t,e,n){"use strict";n.__esModule=!0,n["default"]={pool:{size:navigator.hardwareConcurrency||8}},e.exports=n["default"]},{}],1:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}var o=t("./index"),i=r(o);"object"==typeof window&&(window.thread=i["default"]),"function"==typeof define?define([],function(){return i["default"]}):"object"==typeof e&&(e.exports=i["default"])},{"./index":3}],"./worker":[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function s(t,e){return t&&e?"/"===t.charAt(t.length-1)||"/"===e.charAt(0)?t+e:t+"/"+e:t+e}function a(t){var e=d.getConfig().basepath.web;return e?s(e,t):t}function u(t){for(var e=[],n=0;"undefined"!=typeof t[n];)e.push(t[n]),n++;return e}function c(t){if(t.stack)console.error(t.stack);else if(t.message&&t.filename&&t.lineno){var e=t.filename.match(/^data:text\/javascript/)&&t.filename.length>50?t.filename.substr(0,50)+"...":t.filename;console.error(t.message+" @"+e+":"+t.lineno)}else console.error(t)}n.__esModule=!0;var f=t("eventemitter3"),l=r(f),h=t("./slave-code-uri"),p=r(h),d=t("../config");if("object"!=typeof window.Worker&&"function"!=typeof window.Worker)throw new Error("Browser does not support web workers!");var y=function(t){function e(){var n=arguments.length<=0||void 0===arguments[0]?null:arguments[0],r=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];o(this,e),t.call(this),this.currentRunnable=null,this.currentImportScripts=[],this.initWorker(),this.worker.addEventListener("message",this.handleMessage.bind(this)),this.worker.addEventListener("error",this.handleError.bind(this)),n&&this.run(n,r)}return i(e,t),e.prototype.initWorker=function(){try{this.worker=new window.Worker(p["default"])}catch(t){var e=d.getConfig().fallback.slaveScriptUrl;if(!e)throw t;this.worker=new window.Worker(p["default"])}},e.prototype.run=function(t){var e=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];return this.alreadyInitializedToRun(t,e)?this:("function"==typeof t?this.runMethod(t,e):this.runScripts(t,e),this.currentRunnable=t,this.currentImportScripts=e,this)},e.prototype.runMethod=function(t,e){var n=t.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:e.map(a)})},e.prototype.runScripts=function(t,e){if(!t)throw new Error("Must pass a function or a script URL to run().");this.worker.postMessage({initByScripts:!0,scripts:e.concat([t]).map(a)})},e.prototype.send=function(t){var e=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];return this.worker.postMessage({doRun:!0,param:t},e),this},e.prototype.kill=function(){return this.worker.terminate(),this.emit("exit"),this},e.prototype.promise=function(){var t=this;return new Promise(function(e,n){t.once("message",e).once("error",n)})},e.prototype.alreadyInitializedToRun=function(t,e){var n=this.currentRunnable===t,r=this.currentImportScripts===e||0===e.length&&0===this.currentImportScripts.length;return n&&r},e.prototype.handleMessage=function(t){if(t.data.error)this.handleError(t.data.error);else if(t.data.progress)this.handleProgress(t.data.progress);else{var e=u(t.data.response);this.emit.apply(this,["message"].concat(e)),this.emit.apply(this,["done"].concat(e))}},e.prototype.handleProgress=function(t){this.emit("progress",t)},e.prototype.handleError=function(t){this.listeners("error",!0)||c(t),t.preventDefault&&t.preventDefault(),this.emit("error",t)},e}(l["default"]);n["default"]=y,e.exports=n["default"]},{"../config":2,"./slave-code-uri":6,eventemitter3:8}],2:[function(t,e,n){"use strict";function r(t,e){var n=arguments.length<=2||void 0===arguments[2]?[]:arguments[2];Object.keys(e).forEach(function(o){var i=e[o],s=n.concat([o]);if("object"==typeof i){if("undefined"!=typeof t[o]&&"object"!=typeof t[o])throw new Error("Expected config property not to be an object: "+s.join("."));r(t[o],i,s)}else{if("object"==typeof t[o])throw new Error("Expected config property to be an object: "+s.join("."));t[o]=i}})}function o(){return a.get()}function i(){return a.set.apply(a,arguments)}n.__esModule=!0,n.getConfig=o,n.setConfig=i;var s={basepath:{node:"",web:""},fallback:{slaveScriptUrl:""}},a={get:function(){return s},set:function(t){if("object"!=typeof t)throw new Error("Expected config object.");r(s,t)}};n["default"]=a},{}],3:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(){var t=arguments.length<=0||void 0===arguments[0]?null:arguments[0],e=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];return new h["default"](t,e)}n.__esModule=!0,n.spawn=o,t("native-promise-only");var i=t("./config"),s=r(i),a=t("./defaults"),u=r(a),c=t("./pool"),f=r(c),l=t("./worker"),h=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:h["default"]}},{"./config":2,"./defaults":"./defaults","./pool":5,"./worker":"./worker","native-promise-only":9}],4:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}n.__esModule=!0;var s=t("eventemitter3"),a=r(s),u=function(t){function e(n){o(this,e),t.call(this),this.pool=n,this.thread=null,this.runArgs=[],this.sendArgs=[],n.emit("newJob",this)}return i(e,t),e.prototype.run=function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];if(0===e.length)throw new Error("Cannot call .run() without arguments.");return this.runArgs=e,this},e.prototype.send=function(){if(0===this.runArgs.length)throw new Error("Cannot .send() before .run().");for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];return this.sendArgs=e,this.emit("readyToRun"),this},e.prototype.executeOn=function(t){var e,n;return(e=(n=t.once("message",this.emit.bind(this,"done")).once("error",this.emit.bind(this,"error"))).run.apply(n,this.runArgs)).send.apply(e,this.sendArgs),this.thread=t,this.emit("threadChanged"),this},e.prototype.promise=function(){var t=this;return new Promise(function(e){t.thread?e(t.thread.promise()):t.once("threadChanged",function(){e(t.thread.promise())})})},e}(a["default"]);n["default"]=u,e.exports=n["default"]},{eventemitter3:8}],5:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}n.__esModule=!0;var s=t("eventemitter3"),a=r(s),u=t("./job"),c=r(u),f=t("./defaults"),l=r(f),h=t("./"),p=function(t){function e(n){var r=this;o(this,e),t.call(this),this.threads=e.spawn(n||l["default"].pool.size),this.idleThreads=this.threads.slice(),this.jobQueue=[],this.runArgs=[],this.on("newJob",function(t){return r.handleNewJob(t)}),this.on("threadAvailable",function(){return r.dequeue()})}return i(e,t),e.prototype.run=function(t){return this.runArgs=t,this},e.prototype.send=function(){if(!this.runArgs)throw new Error("Pool.send() called without prior Pool.run(). You need to define what to run first.");var t=new c["default"](this);return t.run(this.runArgs),t.send.apply(t,arguments)},e.prototype.killAll=function(){this.threads.forEach(function(t){t.kill()})},e.prototype.queueJob=function(t){this.jobQueue.push(t),this.dequeue()},e.prototype.dequeue=function(){var t=this;if(0!==this.jobQueue.length&&0!==this.idleThreads.length){var e=this.jobQueue.shift(),n=this.idleThreads.shift();e.once("done",function(){for(var r=arguments.length,o=Array(r),i=0;i<r;i++)o[i]=arguments[i];return t.handleJobSuccess.apply(t,[n,e].concat(o))}).once("error",function(){for(var r=arguments.length,o=Array(r),i=0;i<r;i++)o[i]=arguments[i];return t.handleJobError.apply(t,[n,e].concat(o))}),e.executeOn(n)}},e.prototype.handleNewJob=function(t){var e=this;this.lastCreatedJob=t,t.once("readyToRun",function(){return e.queueJob(t)})},e.prototype.handleJobSuccess=function(t,e){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",e].concat(r)),this.handleJobDone(t)},e.prototype.handleJobError=function(t,e,n){this.emit("error",e,n),this.handleJobDone(t)},e.prototype.handleJobDone=function(t){var e=this;this.idleThreads.push(t),this.emit("threadAvailable"),this.idleThreads.length===this.threads.length&&setTimeout(function(){e.emit("finished")},0)},e}(a["default"]);n["default"]=p,p.spawn=function(t){for(var e=[],n=0;n<t;n++)e.push(h.spawn());return e},e.exports=n["default"]},{"./":3,"./defaults":"./defaults","./job":4,eventemitter3:8}],6:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}n.__esModule=!0;var o=t("./slave-code"),i=r(o),s="data:text/javascript;charset=utf-8,"+encodeURI(i["default"]),a=window.createBlobURL||window.createObjectURL;if(!a){var u=window.URL||window.webkitURL;if(!u)throw new Error("No Blob creation implementation found.");a=u.createObjectURL}if("function"==typeof window.BlobBuilder&&"function"==typeof a){var c=new window.BlobBuilder;c.append(i["default"]),s=a(c.getBlob())}else if("function"==typeof window.Blob&&"function"==typeof a){var f=new window.Blob([i["default"]],{type:"text/javascript"});s=a(f)}n["default"]=s,e.exports=n["default"]},{"./slave-code":7}],7:[function(t,e,n){e.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 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\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 var method = event.data.method;\n this.module.exports = Function.apply(null, method.args.concat(method.body));\n\n if (scripts && scripts.length > 0) {\n importScripts.apply(null, scripts);\n }\n }\n\n if (event.data.doRun) {\n var handler = this.module.exports;\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 handler.call(this, event.data.param, preparedHandlerDone, handlerProgress.bind(this));\n }\n}.bind(self);\n"},{}],8:[function(t,e,n){"use strict";function r(t,e,n){this.fn=t,this.context=e,this.once=n||!1}function o(){}var i=Object.prototype.hasOwnProperty,s="function"!=typeof Object.create&&"~";o.prototype._events=void 0,o.prototype.eventNames=function(){var t,e=this._events,n=[];if(!e)return n;for(t in e)i.call(e,t)&&n.push(s?t.slice(1):t);return Object.getOwnPropertySymbols?n.concat(Object.getOwnPropertySymbols(e)):n},o.prototype.listeners=function(t,e){var n=s?s+t:t,r=this._events&&this._events[n];if(e)return!!r;if(!r)return[];if(r.fn)return[r.fn];for(var o=0,i=r.length,a=new Array(i);o<i;o++)a[o]=r[o].fn;return a},o.prototype.emit=function(t,e,n,r,o,i){var a=s?s+t:t;if(!this._events||!this._events[a])return!1;var u,c,f=this._events[a],l=arguments.length;if("function"==typeof f.fn){switch(f.once&&this.removeListener(t,f.fn,void 0,!0),l){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,e),!0;case 3:return f.fn.call(f.context,e,n),!0;case 4:return f.fn.call(f.context,e,n,r),!0;case 5:return f.fn.call(f.context,e,n,r,o),!0;case 6:return f.fn.call(f.context,e,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 h,p=f.length;for(c=0;c<p;c++)switch(f[c].once&&this.removeListener(t,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,e);break;case 3:f[c].fn.call(f[c].context,e,n);break;default:if(!u)for(h=1,u=new Array(l-1);h<l;h++)u[h-1]=arguments[h];f[c].fn.apply(f[c].context,u)}}return!0},o.prototype.on=function(t,e,n){var o=new r(e,n||this),i=s?s+t:t;return this._events||(this._events=s?{}:Object.create(null)),this._events[i]?this._events[i].fn?this._events[i]=[this._events[i],o]:this._events[i].push(o):this._events[i]=o,this},o.prototype.once=function(t,e,n){var o=new r(e,n||this,(!0)),i=s?s+t:t;return this._events||(this._events=s?{}:Object.create(null)),this._events[i]?this._events[i].fn?this._events[i]=[this._events[i],o]:this._events[i].push(o):this._events[i]=o,this},o.prototype.removeListener=function(t,e,n,r){var o=s?s+t:t;if(!this._events||!this._events[o])return this;var i=this._events[o],a=[];if(e)if(i.fn)(i.fn!==e||r&&!i.once||n&&i.context!==n)&&a.push(i);else for(var u=0,c=i.length;u<c;u++)(i[u].fn!==e||r&&!i[u].once||n&&i[u].context!==n)&&a.push(i[u]);return a.length?this._events[o]=1===a.length?a[0]:a:delete this._events[o],this},o.prototype.removeAllListeners=function(t){return this._events?(t?delete this._events[s?s+t:t]:this._events=s?{}:Object.create(null),this):this},o.prototype.off=o.prototype.removeListener,o.prototype.addListener=o.prototype.on,o.prototype.setMaxListeners=function(){return this},o.prefixed=s,"undefined"!=typeof e&&(e.exports=o)},{}],9:[function(t,e,n){(function(t){!function(t,n,r){n[t]=n[t]||r(),"undefined"!=typeof e&&e.exports?e.exports=n[t]:"function"==typeof define&&define.amd&&define(function(){return n[t]})}("Promise","undefined"!=typeof t?t:this,function(){"use strict";function t(t,e){h.add(t,e),l||(l=d(h.drain))}function e(t){var e,n=typeof t;return null==t||"object"!=n&&"function"!=n||(e=t.then),"function"==typeof e&&e}function n(){for(var t=0;t<this.chain.length;t++)r(this,1===this.state?this.chain[t].success:this.chain[t].failure,this.chain[t]);this.chain.length=0}function r(t,n,r){var o,i;try{n===!1?r.reject(t.msg):(o=n===!0?t.msg:n.call(void 0,t.msg),o===r.promise?r.reject(TypeError("Promise-chain cycle")):(i=e(o))?i.call(o,r.resolve,r.reject):r.resolve(o))}catch(s){r.reject(s)}}function o(r){var s,u=this;if(!u.triggered){u.triggered=!0,u.def&&(u=u.def);try{(s=e(r))?t(function(){var t=new a(u);try{s.call(r,function(){o.apply(t,arguments)},function(){i.apply(t,arguments)})}catch(e){i.call(t,e)}}):(u.msg=r,u.state=1,u.chain.length>0&&t(n,u))}catch(c){i.call(new a(u),c)}}}function i(e){var r=this;r.triggered||(r.triggered=!0,r.def&&(r=r.def),r.msg=e,r.state=2,r.chain.length>0&&t(n,r))}function s(t,e,n,r){for(var o=0;o<e.length;o++)!function(o){t.resolve(e[o]).then(function(t){n(o,t)},r)}(o)}function a(t){this.def=t,this.triggered=!1}function u(t){this.promise=t,this.state=0,this.triggered=!1,this.chain=[],this.msg=void 0}function c(e){if("function"!=typeof e)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(e,o){var i={success:"function"!=typeof e||e,failure:"function"==typeof o&&o};return i.promise=new this.constructor(function(t,e){if("function"!=typeof t||"function"!=typeof e)throw TypeError("Not a function");i.resolve=t,i.reject=e}),r.chain.push(i),0!==r.state&&t(n,r),i.promise},this["catch"]=function(t){return this.then(void 0,t)};try{e.call(void 0,function(t){o.call(r,t)},function(t){i.call(r,t)})}catch(s){i.call(r,s)}}var f,l,h,p=Object.prototype.toString,d="undefined"!=typeof setImmediate?function(t){return setImmediate(t)}:setTimeout;try{Object.defineProperty({},"x",{}),f=function(t,e,n,r){return Object.defineProperty(t,e,{value:n,writable:!0,configurable:r!==!1})}}catch(y){f=function(t,e,n){return t[e]=n,t}}h=function(){function t(t,e){this.fn=t,this.self=e,this.next=void 0}var e,n,r;return{add:function(o,i){r=new t(o,i),n?n.next=r:e=r,n=r,r=void 0},drain:function(){var t=e;for(e=n=l=void 0;t;)t.fn.call(t.self),t=t.next}}}();var v=f({},"constructor",c,!1);return c.prototype=v,f(v,"__NPO__",0,!1),f(c,"resolve",function(t){var e=this;return t&&"object"==typeof t&&1===t.__NPO__?t:new e(function(e,n){if("function"!=typeof e||"function"!=typeof n)throw TypeError("Not a function");e(t)})}),f(c,"reject",function(t){return new this(function(e,n){if("function"!=typeof e||"function"!=typeof n)throw TypeError("Not a function");n(t)})}),f(c,"all",function(t){var e=this;return"[object Array]"!=p.call(t)?e.reject(TypeError("Not an array")):0===t.length?e.resolve([]):new e(function(n,r){if("function"!=typeof n||"function"!=typeof r)throw TypeError("Not a function");var o=t.length,i=Array(o),a=0;s(e,t,function(t,e){i[t]=e,++a===o&&n(i)},r)})}),f(c,"race",function(t){var e=this;return"[object Array]"!=p.call(t)?e.reject(TypeError("Not an array")):new e(function(n,r){if("function"!=typeof n||"function"!=typeof r)throw TypeError("Not a function");s(e,t,function(t,e){n(e)},r)})}),c})}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1]); |
@@ -29,2 +29,4 @@ 'use strict'; | ||
function Pool(threads) { | ||
var _this = this; | ||
_classCallCheck(this, Pool); | ||
@@ -38,3 +40,8 @@ | ||
this.on('newJob', this.handleNewJob.bind(this)); | ||
this.on('newJob', function (job) { | ||
return _this.handleNewJob(job); | ||
}); | ||
this.on('threadAvailable', function () { | ||
return _this.dequeue(); | ||
}); | ||
} | ||
@@ -69,4 +76,6 @@ | ||
Pool.prototype.dequeue = function dequeue() { | ||
var _this2 = this; | ||
if (this.jobQueue.length === 0 || this.idleThreads.length === 0) { | ||
return this.once('threadAvailable', this.dequeue); | ||
return; | ||
} | ||
@@ -77,4 +86,16 @@ | ||
job.once('done', this.handleJobSuccess.bind(this, thread, job)).once('error', this.handleJobError.bind(this, thread, job)); | ||
job.once('done', function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return _this2.handleJobSuccess.apply(_this2, [thread, job].concat(args)); | ||
}).once('error', function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return _this2.handleJobError.apply(_this2, [thread, job].concat(args)); | ||
}); | ||
job.executeOn(thread); | ||
@@ -84,9 +105,13 @@ }; | ||
Pool.prototype.handleNewJob = function handleNewJob(job) { | ||
var _this3 = this; | ||
this.lastCreatedJob = job; | ||
job.once('readyToRun', this.queueJob.bind(this, job)); // triggered by job.send() | ||
job.once('readyToRun', function () { | ||
return _this3.queueJob(job); | ||
}); // triggered by job.send() | ||
}; | ||
Pool.prototype.handleJobSuccess = function handleJobSuccess(thread, job) { | ||
for (var _len = arguments.length, responseArgs = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
responseArgs[_key - 2] = arguments[_key]; | ||
for (var _len3 = arguments.length, responseArgs = Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) { | ||
responseArgs[_key3 - 2] = arguments[_key3]; | ||
} | ||
@@ -104,3 +129,3 @@ | ||
Pool.prototype.handleJobDone = function handleJobDone(thread) { | ||
var _this = this; | ||
var _this4 = this; | ||
@@ -113,3 +138,3 @@ this.idleThreads.push(thread); | ||
setTimeout(function () { | ||
_this.emit('finished'); | ||
_this4.emit('finished'); | ||
}, 0); | ||
@@ -116,0 +141,0 @@ } |
@@ -25,5 +25,15 @@ 'use strict'; | ||
function joinPaths(path1, path2) { | ||
if (!path1 || !path2) { | ||
return path1 + path2; | ||
} else if (path1.charAt(path1.length - 1) === '/' || path2.charAt(0) === '/') { | ||
return path1 + path2; | ||
} else { | ||
return path1 + '/' + path2; | ||
} | ||
} | ||
function prependScriptUrl(scriptUrl) { | ||
var prefix = _config.getConfig().basepath.web; | ||
return prefix ? prefix + '/' + scriptUrl : scriptUrl; | ||
return prefix ? joinPaths(prefix, scriptUrl) : scriptUrl; | ||
} | ||
@@ -65,2 +75,6 @@ | ||
// used by `run()` to decide if the worker must be re-initialized | ||
this.currentRunnable = null; | ||
this.currentImportScripts = []; | ||
this.initWorker(); | ||
@@ -93,2 +107,7 @@ this.worker.addEventListener('message', this.handleMessage.bind(this)); | ||
if (this.alreadyInitializedToRun(toRun, importScripts)) { | ||
// don't re-initialize with the new logic if it already has been | ||
return this; | ||
} | ||
if (typeof toRun === 'function') { | ||
@@ -99,2 +118,6 @@ this.runMethod(toRun, importScripts); | ||
} | ||
this.currentRunnable = toRun; | ||
this.currentImportScripts = importScripts; | ||
return this; | ||
@@ -151,2 +174,9 @@ }; | ||
Worker.prototype.alreadyInitializedToRun = function alreadyInitializedToRun(toRun, importScripts) { | ||
var runnablesMatch = this.currentRunnable === toRun; | ||
var importScriptsMatch = this.currentImportScripts === importScripts || importScripts.length === 0 && this.currentImportScripts.length === 0; | ||
return runnablesMatch && importScriptsMatch; | ||
}; | ||
Worker.prototype.handleMessage = function handleMessage(event) { | ||
@@ -153,0 +183,0 @@ if (event.data.error) { |
{ | ||
"name": "threads", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "threads", |
@@ -338,2 +338,7 @@ # threads.js | ||
### 0.7.0 | ||
Fixes a critical issue that prevented thread pools from running all jobs. | ||
Also brings some major performance improvements for browser (web worker) - based setups. | ||
### 0.6.1 | ||
@@ -340,0 +345,0 @@ |
@@ -14,4 +14,4 @@ import EventEmitter from 'eventemitter3'; | ||
this.on('newJob', this.handleNewJob.bind(this)); | ||
this.on('newJob', (job) => this.handleNewJob(job)); | ||
this.on('threadAvailable', () => this.dequeue()); | ||
} | ||
@@ -47,3 +47,3 @@ | ||
if (this.jobQueue.length === 0 || this.idleThreads.length === 0) { | ||
return this.once('threadAvailable', this.dequeue); | ||
return; | ||
} | ||
@@ -55,4 +55,4 @@ | ||
job | ||
.once('done', this.handleJobSuccess.bind(this, thread, job)) | ||
.once('error', this.handleJobError.bind(this, thread, job)); | ||
.once('done', (...args) => this.handleJobSuccess(thread, job, ...args)) | ||
.once('error', (...args) => this.handleJobError(thread, job, ...args)); | ||
@@ -64,3 +64,3 @@ job.executeOn(thread); | ||
this.lastCreatedJob = job; | ||
job.once('readyToRun', this.queueJob.bind(this, job)); // triggered by job.send() | ||
job.once('readyToRun', () => this.queueJob(job)); // triggered by job.send() | ||
} | ||
@@ -67,0 +67,0 @@ |
@@ -12,5 +12,15 @@ import EventEmitter from 'eventemitter3'; | ||
function joinPaths (path1, path2) { | ||
if (!path1 || !path2) { | ||
return path1 + path2; | ||
} else if (path1.charAt(path1.length - 1) === '/' || path2.charAt(0) === '/') { | ||
return path1 + path2; | ||
} else { | ||
return path1 + '/' + path2; | ||
} | ||
} | ||
function prependScriptUrl(scriptUrl) { | ||
const prefix = getConfig().basepath.web; | ||
return prefix ? prefix + '/' + scriptUrl : scriptUrl; | ||
return prefix ? joinPaths(prefix, scriptUrl) : scriptUrl; | ||
} | ||
@@ -48,2 +58,6 @@ | ||
// used by `run()` to decide if the worker must be re-initialized | ||
this.currentRunnable = null; | ||
this.currentImportScripts = []; | ||
this.initWorker(); | ||
@@ -74,2 +88,7 @@ this.worker.addEventListener('message', this.handleMessage.bind(this)); | ||
run(toRun, importScripts = []) { | ||
if (this.alreadyInitializedToRun(toRun, importScripts)) { | ||
// don't re-initialize with the new logic if it already has been | ||
return this; | ||
} | ||
if (typeof toRun === 'function') { | ||
@@ -80,2 +99,6 @@ this.runMethod(toRun, importScripts); | ||
} | ||
this.currentRunnable = toRun; | ||
this.currentImportScripts = importScripts; | ||
return this; | ||
@@ -128,2 +151,10 @@ } | ||
alreadyInitializedToRun(toRun, importScripts) { | ||
const runnablesMatch = this.currentRunnable === toRun; | ||
const importScriptsMatch = this.currentImportScripts === importScripts | ||
|| (importScripts.length === 0 && this.currentImportScripts.length === 0); | ||
return runnablesMatch && importScriptsMatch; | ||
} | ||
handleMessage(event) { | ||
@@ -130,0 +161,0 @@ if (event.data.error) { |
@@ -230,2 +230,23 @@ import async from 'async'; | ||
it('can run a lot of jobs', (done) => { | ||
const pool = new Pool(3); | ||
let calledJob = 0; | ||
function onDone () { | ||
calledJob++; | ||
} | ||
for (let jobIndex = 0; jobIndex < 50; jobIndex++) { | ||
pool | ||
.run(noop) | ||
.send({ jobIndex }) | ||
.on('done', onDone); | ||
} | ||
pool.once('finished', () => { | ||
expect(calledJob).to.equal(50); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -253,2 +253,20 @@ 'use strict'; | ||
}); | ||
it('can run a lot of jobs', function (done) { | ||
var pool = new _lib.Pool(3); | ||
var calledJob = 0; | ||
function onDone() { | ||
calledJob++; | ||
} | ||
for (var jobIndex = 0; jobIndex < 50; jobIndex++) { | ||
pool.run(noop).send({ jobIndex: jobIndex }).on('done', onDone); | ||
} | ||
pool.once('finished', function () { | ||
(0, _expectJs2['default'])(calledJob).to.equal(50); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
202794
3740
360