kelda-js
Version:
A thread pool for the browser, built on top of Web Workers
2 lines • 10.7 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.MyLibrary=e():t.MyLibrary=e()}(global,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";r.r(e);var n,o=function(){for(var t=0,e=0,r=arguments.length;e<r;e++)t+=arguments[e].length;var n=Array(t),o=0;for(e=0;e<r;e++)for(var i=arguments[e],u=0,a=i.length;u<a;u++,o++)n[o]=i[u];return n},i=function(){function t(t){this.isDone=!1,this.args=[],this.workModule=t}return t.prototype.with=function(t){return this.args=t,this},t.prototype.execute=function(){var t=this;return new Promise((function(e,r){try{var n=t.workModule.get();e(n.call.apply(n,o([null],t.args)))}catch(t){r(t)}})).finally((function(){return t.isDone=!0}))},t}();!function(t){t.START="$$_KELDA_START",t.DONE="$$_KELDA_DONE",t.ERROR="$$_KELDA_ERROR"}(n||(n={}));var u,a=function(){function t(t){this.isDone=!1,this.worker=null,this.args=[],this.cleanUp=this.cleanUp.bind(this),this.workModule=t}return t.prototype.with=function(t){return this.args=t,this},t.prototype.execute=function(){return this.url=this.getWorkerUrl(),this.getWorkPromise().finally(this.cleanUp)},t.prototype.cleanUp=function(){var t;this.url&&URL.revokeObjectURL(this.url),null===(t=this.worker)||void 0===t||t.terminate(),this.isDone=!0},t.prototype.getWorkPromise=function(){var t=this;return new Promise((function(e,r){try{t.doWorkInWorker(e,r)}catch(t){r(t)}}))},t.prototype.doWorkInWorker=function(t,e){this.url=this.getWorkerUrl(),this.worker=new Worker(this.url),this.initWorkerMessageHandling(t,e),this.startWork()},t.prototype.startWork=function(){var t;null===(t=this.worker)||void 0===t||t.postMessage({type:n.START,args:this.args})},t.prototype.initWorkerMessageHandling=function(t,e){var r;null===(r=this.worker)||void 0===r||r.addEventListener("message",(function(r){var o=r.data,i=o.type,u=o.result,a=o.error;switch(i){case n.DONE:t(u);break;case n.ERROR:e(a)}}))},t.prototype.getWorkerUrl=function(){var t=this.getWorkerScript(),e=new Blob([t],{type:"text/javascript"});return URL.createObjectURL(e)},t.prototype.getWorkerScript=function(){return"("+function(t,e){var r=!1;self.onmessage=function(n){var o,i;if(!r&&"$$_KELDA_START"===(null===(o=n.data)||void 0===o?void 0:o.type))try{if("object"!=typeof t())throw new Error("Provided work script did not evaluate to a module object");var u=t()[e];if(!u)throw new Error("Export '"+e+"' was not found in provided work module");var a=(null===(i=n.data)||void 0===i?void 0:i.args)||[],s=u.apply(null,a);s instanceof Promise?s.then((function(t){self.postMessage({type:"$$_KELDA_DONE",result:t})})):self.postMessage({type:"$$_KELDA_DONE",result:s})}catch(t){self.postMessage({type:"$$_KELDA_ERROR",error:t})}finally{r=!0}}}+")("+this.workModule+', "'+this.workModule.exportName+'")'},t}(),s=function(){function t(){}return t.getJob=function(t){var e;return(null===(e=window)||void 0===e?void 0:e.Worker)?new a(t):new i(t)},t}(),c=function(){function t(){this.isIdle=!0}return t.prototype.do=function(t){var e=this;return this.isIdle=!1,t.execute().finally((function(){return e.isIdle=!0}))},t}(),l=(u=function(t,e){return(u=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(t,e)},function(t,e){function r(){this.constructor=t}u(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}),f=function(t){function e(e){var r=t.call(this,e)||this;return r.message=e,r.name="KeldaError",r}return l(e,t),e}(Error),h=function(){function t(e){this.queue=[],t.validate(e),this.threads=this.populateThreads(e),this.doFromQueue=this.doFromQueue.bind(this)}return t.validate=function(t){if(t<=0)throw new f("threadPoolDepth must be greater than 0")},t.prototype.schedule=function(t){var e=this,r=this.getThread();return r?r.do(t).finally(this.doFromQueue):new Promise((function(r,n){var o=[t,r,n];e.queue.push(o)}))},t.prototype.doFromQueue=function(){var t=this.getThread();if(t&&this.queue.length){var e=this.queue.shift(),r=e[0],n=e[1],o=e[2];t.do(r).then(n).catch(o).finally(this.doFromQueue)}},t.prototype.populateThreads=function(t){return Array(t).fill(null).map((function(){return new c}))},t.prototype.getThread=function(){return this.threads.find((function(t){return t.isIdle}))},t}(),p=function(){function t(t,e){this.source=t,this.exportName=e}return t.prototype.get=function(){return Function('"use-strict"; return '+this.source+";").call(null)[this.exportName]},t.prototype.toString=function(){return""+("function(){\n return "+this.source+"\n }")},t}(),d=function(t,e,r,n){return new(r||(r=Promise))((function(o,i){function u(t){try{s(n.next(t))}catch(t){i(t)}}function a(t){try{s(n.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(u,a)}s((n=n.apply(t,e||[])).next())}))},y=function(t,e){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=(o=u.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}},v=function(){function t(t,e){void 0===e&&(e="default"),this.url=t,this.exportName=e}return t.prototype.get=function(){return d(this,void 0,void 0,(function(){var t;return y(this,(function(e){switch(e.label){case 0:return this.script=this.script||this.loadScript(this.url),t=p.bind,[4,this.script];case 1:return[2,new(t.apply(p,[void 0,e.sent(),this.exportName]))]}}))}))},t.prototype.loadScript=function(t){return new Promise((function(e,r){var n=new XMLHttpRequest;n.onreadystatechange=function(){var o=n.readyState,i=n.status,u=n.responseText;o===n.DONE&&(i>=200&&i<400?e(u):r(new f("Could not load work from url: '"+t+"'")))},n.open("GET",t),n.send()}))},t}(),w=function(){function t(t){this.exportName="default",this.work=t}return t.prototype.get=function(){return this.work},t.prototype.toString=function(){return"function(){\n return {\n default: "+this.work+"\n }\n }"},t}(),b=function(t,e,r,n){return new(r||(r=Promise))((function(o,i){function u(t){try{s(n.next(t))}catch(t){i(t)}}function a(t){try{s(n.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(u,a)}s((n=n.apply(t,e||[])).next())}))},g=function(t,e){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=(o=u.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}},k={threadPoolDepth:1},m=function(){function t(t){var e=(void 0===t?k:t).threadPoolDepth;this.cache={},this.threadPool=new h(e)}return t.prototype.orderWork=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];return b(this,void 0,void 0,(function(){var r,n,o;return g(this,(function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,this.getWorkModule(t)];case 1:return r=i.sent(),n=s.getJob(r).with(e),[4,this.threadPool.schedule(n)];case 2:return[2,i.sent()];case 3:return o=i.sent(),this.toKeldaError(o),[3,4];case 4:return[2]}}))}))},t.prototype.load=function(t){var e=t.url,r=t.exportName;return b(this,void 0,void 0,(function(){var t,n;return g(this,(function(o){switch(o.label){case 0:return[4,(t=new v(e,r)).get()];case 1:return o.sent(),n=Object.keys(this.cache).length+1,this.cache[n]=t,[2,n]}}))}))},t.prototype.lazy=function(t){var e=t.url,r=t.exportName,n=Object.keys(this.cache).length+1;return this.cache[n]=new v(e,r),n},t.prototype.getWorkModule=function(t){return b(this,void 0,void 0,(function(){var e,r,n;return g(this,(function(o){switch(typeof t){case"function":return[2,new w(t)];case"object":return e=t.url,r=t.exportName,[2,new v(e,r).get()];case"number":if(!(n=this.cache[t]))throw new f("Invalid work id: '"+t+"'");return[2,n.get()]}return[2]}))}))},t.prototype.toKeldaError=function(t){var e=t.message||"Something went wrong while processing work";throw new f(e)},t}();e.default=m}]).default}));
//# sourceMappingURL=bundle.js.map