!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Thread=t():e.Thread=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var s=t[r]={i:r,l:!1,exports:{}};return e[r].call(s.exports,s,s.exports,n),s.l=!0,s.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var s in e)n.d(r,s,function(t){return e[t]}.bind(null,s));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";n.r(t);class r{static funcToString(e){return e.toString().replace(/^[^(\s]+\(/,"function(")}get running(){return!!this._process}get ready(){return!!this._ready}constructor(e,t={},n=[]){if(!(e instanceof Function))throw new Error(e," is not a function");const s=new Array(n.length),o=[];n.forEach((e,t)=>{const n=r._getScript(e);if(n)s[t]=n;else{const n=r._getScriptPromise(e);n.then(e=>s[t]=e),o.push(n)}}),this._disposed=!1,Promise.all(o).then(()=>this._initWorker(e,t,s))}run(e,t,n){return this.cancel(),this.ready?this._worker.postMessage({args:e,transferList:n},n):this._lateRun=(()=>{this._worker.postMessage({args:e,transferList:n},n),delete this._lateRun}),new Promise((e,n)=>{this._process={resolve:e,reject:n,intermediateFunc:t}})}cancel(){this._process&&(this._process.reject({type:"cancel",msg:null}),this._process=null,this.ready&&this.running&&(this._worker.terminate(),this._constructWorker())),delete this._lateRun}dispose(){this.cancel(),this._worker&&this._worker.terminate(),this._ready=!1,this._disposed=!0}_initWorker(e,t,n){this._disposed||(this._cachedScript=`\n // context definition\n ${Object.keys(t).map(e=>{const n=t[e];let s=null;return`const ${e} = ${s=n instanceof Function?r.funcToString(n):JSON.stringify(n)};`}).join("\n")}\n\n // scripts\n ${n.join("\n")}\n\n // self calling function so the thread function\n // doesn't have access to our scope\n ;(function(threadFunc) {\n\n // override the "postMessage" function\n const __postMessage = postMessage\n postMessage = msg => {\n __postMessage({\n type: 'intermediate',\n data: msg\n });\n };\n\n // set the on message function to start a\n // thread run\n onmessage = e => {\n\n const res = threadFunc(e.data.args);\n const doComplete = data => {\n __postMessage({\n type: 'complete',\n data: data\n },\n e.data.transferList)\n };\n\n if (res instanceof Promise) res.then(data => doComplete(data));\n else doComplete(res);\n };\n })(${r.funcToString(e)})\n `,this._constructWorker())}_constructWorker(){const e=new Blob([this._cachedScript],{type:"plain/text"}),t=URL.createObjectURL(e);this._worker=new Worker(t),this._worker.onmessage=(e=>{if("complete"===e.data.type){const t=this._process;this._process=null,t.resolve(e.data.data)}else this._process.intermediateFunc&&this._process.intermediateFunc(e.data.data)}),this._worker.onerror=(e=>{this._process.reject({type:"error",msg:e.message}),this._process=null}),requestAnimationFrame(()=>URL.revokeObjectURL(t)),this._ready=!0,this._lateRun&&this._lateRun()}}r._cachedScripts={},r._scriptPromises={},r._getScript=(e=>e in r._cachedScripts?r._cachedScripts[e]:null),r._getScriptPromise=(e=>e in r._scriptPromises?r._scriptPromises[e]:r._scriptPromises[e]=new Promise((t,n)=>{fetch(e,{credentials:"same-origin"}).then(e=>e.text()).then(n=>{r._cachedScripts[e]=n,t(n)}).catch(t=>{console.error(`Could not load script from '${e}'`),console.error(t)})})),t.default=r}])});