Skip to content

Instantly share code, notes, and snippets.

@icodeforlove
Created July 29, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icodeforlove/305e641e531062789678e1bc050bc36e to your computer and use it in GitHub Desktop.
Save icodeforlove/305e641e531062789678e1bc050bc36e to your computer and use it in GitHub Desktop.
/*! task.js - 0.0.17 - clientside/minified */
var task=function(t){function e(n){if(r[n])return r[n].exports;var i=r[n]={exports:{},id:n,loaded:!1};return t[n].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{"default":t}}var i=r(1),o=n(i),s=r(3),a=n(s),u=r(4),l=n(u),c={maxWorkers:navigator.hardwareConcurrency},f=r((0,o["default"])()?5:7);t.exports=new a["default"](c,f),t.exports.defaults=(0,l["default"])(c,f,a["default"])},function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{"default":t}}var i=r(2),o=n(i);t.exports=function(){if("undefined"!=typeof Worker&&(window.URL||window.webkitURL))try{var t=new Worker((0,o["default"])(function(){}));return t.terminate(),!0}catch(e){}return!1}},function(t,e){"use strict";t.exports=function(t){var e,r=t.toString();r=r.substring(r.indexOf("{")+1,r.lastIndexOf("}"));try{e=new Blob([r],{type:"text/javascript"})}catch(n){window.BlobBuilder=window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder,e=new BlobBuilder,e.append(r),e=e.getBlob()}return(window.URL||window.webkitURL).createObjectURL(e)}},function(t,e){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t},i=function(){function t(e,n){var i=this;if(r(this,t),this._next=function(){if(i._queue.length){var t=i._getWorker();if(!t)return void setTimeout(i._next,0);var e=i._queue.shift();i._log("sending tid("+e.id+") to wid("+t.id+")"),t.run(e)}},this._onWorkerTaskComplete=function(){i._next()},this._onWorkerExit=function(t){i._log("worker died, reissuing task"),i._workers=i._workers.filter(function(e){return e!=t}),t.tasks.forEach(function(t){i._queue.push(t.$options)}),i._next()},e=e||{},this.id=++t.managerCount,this._WorkerProxy=n,this._logger=e.logger||console.log,this._maxWorkers=e.maxWorkers||4,this._idleTimeout=e.idleTimeout!==!1&&e.idleTimeout,this._idleCheckInterval=e.idleCheckInterval||1e3,this._warmStart=e.warmStart||!1,this._globals=e.globals,this._globalsInitializationFunction=e.initialize,this._debug=e.debug,this._log("creating new pool : "+JSON.stringify(e)),this._workers=[],this._workersInitializing=[],this._queue=[],this._onWorkerTaskComplete=this._onWorkerTaskComplete.bind(this),this._flushIdleWorkers=this._flushIdleWorkers.bind(this),this._totalWorkersCreated=0,this._warmStart){this._log("warm starting workers");for(var o=0;o<this._maxWorkers;o++)this._createWorker()}}return t.prototype._log=function(t){this._debug&&this._logger("task.js:manager[mid("+this.id+")] "+t)},t.prototype.getActiveWorkerCount=function(){return this._workersInitializing.length+this._workers.length},t.prototype.run=function(e){if(this._idleTimeout&&"number"!=typeof this._idleCheckIntervalID&&(this._idleCheckIntervalID=setInterval(this._flushIdleWorkers,this._idleCheckInterval)),!e.arguments||"undefined"==typeof e.arguments.length)throw new Error('task.js: "arguments" is required property, and it must be an array/array-like');if(!e["function"]&&("function"!=typeof e["function"]||"string"!=typeof e["function"]))throw new Error('task.js: "function" is required property, and it must be a string or a function');return"object"===n(e.arguments)&&(e.arguments=Array.prototype.slice.call(e.arguments)),e.id=++t.taskCount,this._log("added tid("+e.id+") to the queue"),e.callback?(this._queue.push(e),void this._next()):new Promise(function(t,r){e.resolve=t,e.reject=r,this._queue.push(e),this._next()}.bind(this))},t.prototype._runOnWorker=function(e,r,n){return new Promise(function(i,o){e.run({id:++t.taskCount,arguments:r,"function":n,resolve:i,reject:o})})},t.prototype.wrap=function(t){return function(){var e=Array.from(arguments),r=null;return"function"==typeof e[e.length-1]&&(r=e.slice(-1).pop(),e=e.slice(0,-1)),this.run({arguments:e,"function":t,callback:r})}.bind(this)},t.prototype.terminate=function(){this._log("terminated"),this._idleTimeout&&"number"==typeof this._idleCheckIntervalID&&(clearInterval(this._idleCheckIntervalID),this._idleCheckIntervalID=null),this._workers.forEach(function(t){t.terminate()}),this._workers=[]},t.prototype._flushIdleWorkers=function(){this._log("flushing idle workers"),this._workers=this._workers.filter(function(t){return!(0===t.tasks.length&&new Date-t.lastTaskTimestamp>this._idleTimeout)||(t.terminate(),!1)},this)},t.prototype._getWorker=function(){var t=this._workers.filter(function(t){return 0===t.tasks.length});return t.length?t[0]:this._workers.length<this._maxWorkers&&0===this._workersInitializing.length?this._createWorker():null},t.prototype._createWorker=function(){var t=++this._totalWorkersCreated,e=new this._WorkerProxy({debug:this._debug,logger:this._logger,id:t,managerId:this.id,onTaskComplete:this._onWorkerTaskComplete,onExit:this._onWorkerExit});if(this._globalsInitializationFunction||this._globals){this._log("running global initialization code");var r=("\n\t\t\t\tfunction (_globals) {\n\t\t\t\t\tglobals = ("+(this._globalsInitializationFunction||function(t){return t}).toString()+")(_globals || {});\n\t\t\t\t}\n\t\t\t").trim();return this._workersInitializing.push(e),this._runOnWorker(e,[this._globals||{}],r).then(function(){this._workersInitializing=this._workersInitializing.filter(function(t){return t!=e}),this._workers.push(e)}.bind(this)),null}return this._workers.push(e),e},t}();i.managerCount=0,i.taskCount=0,t.exports=i},function(t,e){"use strict";t.exports=function(t,e,r){return function(n,i){var o={};return Object.keys(t).forEach(function(e){return o[e]=t[e]}),Object.keys(n).forEach(function(t){return o[t]=n[t]}),new r(o,i||e)}}},function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{"default":t}}function i(t,e){for(var r=Object.getOwnPropertyNames(e),n=0;n<r.length;n++){var i=r[n],o=Object.getOwnPropertyDescriptor(e,i);o&&o.configurable&&void 0===t[i]&&Object.defineProperty(t,i,o)}return t}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function s(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function a(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):i(t,e))}var u=r(2),l=n(u),c=r(6),f=n(c),h=function(t){function e(r){o(this,e);var n=s(this,t.apply(this,arguments));return n.WORKER_SOURCE="function () {\n\t\tonmessage = function (event) {\n\t\t\tvar message = event.data;\n\n\t\t\tvar args = Object.keys(message).filter(function (key) {\n\t\t\t\treturn key.match(/^argument/);\n\t\t\t}).sort(function (a, b) {\n\t\t\t\treturn parseInt(a.slice(8), 10) - parseInt(b.slice(8), 10);\n\t\t\t}).map(function (key) {\n\t\t\t\treturn message[key];\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tpostMessage({id: message.id, result: eval('(' + message.func + ')').apply(null, args)});\n\t\t\t} catch (error) {\n\t\t\t\tpostMessage({id: message.id, error: error.message});\n\t\t\t}\n\t\t}\n\t}",n._onMessage=function(t){var e=t.data;n.handleWorkerMessage(e)},n.postMessage=function(t,e){n._log("sending tid("+t.id+") to worker process"),n._worker.postMessage(t,e)},n.terminate=function(){n._log("terminated"),n._worker.terminate()},n._worker=new Worker((0,l["default"])(n.WORKER_SOURCE)),n._worker.addEventListener("message",n._onMessage),n._log("initialized"),n}return a(e,t),e.prototype._log=function(t){this._debug&&this._logger("task.js:worker[mid("+this.managerId+") wid("+this.id+")]: "+t)},e}(f["default"]);t.exports=h},function(t,e){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var n=function(){function t(e){var n=this;r(this,t),this.handleWorkerExit=function(){n._log("killed"),n._onExit(n)},this.handleWorkerMessage=function(t){var e=null;if(n.tasks.some(function(r,n){if(t.id===r.id)return e=n,!0}),null!==e){var r=n.tasks[e];t.error?(n._log("tid("+r.id+") has thrown an error "+t.error),r.callback?r.callback(new Error("task.js: "+t.error)):r.reject(new Error("task.js: "+t.error))):(n._log("tid("+r.id+") has completed"),r.callback?r.callback(null,t.result):r.resolve(t.result)),n._onTaskComplete(n),n.tasks.splice(e,1)}},this.id=e.id,this.managerId=e.managerId,this._debug=e.debug,this._logger=e.logger,this.tasks=[],this.lastTaskTimestamp=null,this._onTaskComplete=e.onTaskComplete,this._onExit=e.onExit}return t.prototype.run=function(t){this.lastTaskTimestamp=new Date;var e={id:t.id,resolve:t.resolve,reject:t.reject,callback:t.callback,$options:t};this.tasks.push(e);var r={id:e.id,func:String(t["function"])};Object.keys(t.arguments).forEach(function(e,n){r["argument"+n]=t.arguments[n]}),this.postMessage(r,t.transferables)},t.prototype._purgeTasks=function(t){this.tasks.forEach(function(e){e.callback?e.callback(t):e.reject(t)}),this.tasks=[]},t}();t.exports=n},function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{"default":t}}function i(t,e){for(var r=Object.getOwnPropertyNames(e),n=0;n<r.length;n++){var i=r[n],o=Object.getOwnPropertyDescriptor(e,i);o&&o.configurable&&void 0===t[i]&&Object.defineProperty(t,i,o)}return t}function o(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)}function s(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(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):i(t,e))}var l=r(6),c=n(l),f=function(t){function e(){s(this,e);var r=a(this,t.apply(this,arguments));return r.postMessage=function(t,e){r._setTimeoutID=setTimeout(function(){var e=Object.keys(t).filter(function(t){return t.match(/^argument/)}).sort(function(t,e){return parseInt(t.slice(8),10)-parseInt(e.slice(8),10)}).map(function(e){return t[e]}),n=t.func.substring(t.func.indexOf("{")+1,t.func.lastIndexOf("}")),i=t.func.substring(t.func.indexOf("(")+1,t.func.indexOf(")")).split(","),s=new(Function.prototype.bind.apply(Function,[null].concat(o(i),[n])));try{var a=s.apply(void 0,o(e));r.handleWorkerMessage({id:t.id,result:a})}catch(u){r.handleWorkerMessage({id:t.id,error:u.message})}},1)},r.terminate=function(){clearTimeout(r._setTimeoutID),r._setTimeoutID=null},r._setTimeoutID=null,r}return u(e,t),e.prototype._log=function(t){this._debug&&this._logger("task.js:worker[mid("+this.managerId+") wid("+this.id+")]: "+t)},e}(c["default"]);t.exports=f}]);
//# sourceMappingURL=task.min.js.map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment