UNPKG

async-array-buffer

Version:

This module allows to allocate and deallocate ArrayBuffers without blocking the main thread.

3 lines 2.87 kB
// This is the minified and stringified code of the async-array-buffer-worker package. export const worker = `(()=>{var e={455:function(e,t){!function(e){"use strict";var t=function(e){return function(t){var r=e(t);return t.add(r),r}},r=function(e){return function(t,r){return e.set(t,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,s=2*o,a=function(e,t){return function(r){var a=t.get(r),i=void 0===a?r.size:a<s?a+1:0;if(!r.has(i))return e(r,i);if(r.size<o){for(;r.has(i);)i=Math.floor(Math.random()*s);return e(r,i)}if(r.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;r.has(i);)i=Math.floor(Math.random()*n);return e(r,i)}},i=new WeakMap,u=r(i),c=a(u,i),l=t(c);e.addUniqueNumber=l,e.generateUniqueNumber=c}(t)}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n].call(s.exports,s,s.exports,r),s.exports}(()=>{"use strict";const e=-32603,t=-32602,n=-32601,o=(e,t)=>Object.assign(new Error(e),{status:t}),s=t=>o('The handler of the method called "'.concat(t,'" returned an unexpected result.'),e),a=(t,r)=>async a=>{let{data:{id:i,method:u,params:c}}=a;const l=r[u];try{if(void 0===l)throw(e=>o('The requested method called "'.concat(e,'" is not supported.'),n))(u);const r=void 0===c?l():l(c);if(void 0===r)throw(t=>o('The handler of the method called "'.concat(t,'" returned no required result.'),e))(u);const a=r instanceof Promise?await r:r;if(null===i){if(void 0!==a.result)throw s(u)}else{if(void 0===a.result)throw s(u);const{result:e,transferables:r=[]}=a;t.postMessage({id:i,result:e},r)}}catch(e){const{message:r,status:n=-32603}=e;t.postMessage({error:{code:n,message:r},id:i})}};var i=r(455);const u=new Map,c=(e,r,n)=>({...r,connect:t=>{let{port:n}=t;n.start();const o=e(n,r),s=(0,i.generateUniqueNumber)(u);return u.set(s,(()=>{o(),n.close(),u.delete(s)})),{result:s}},disconnect:e=>{let{portId:r}=e;const n=u.get(r);if(void 0===n)throw(e=>o('The specified parameter called "portId" with the given value "'.concat(e,'" does not identify a port connected to this worker.'),t))(r);return n(),{result:null}},isSupported:async()=>{if(await new Promise((e=>{const t=new ArrayBuffer(0),{port1:r,port2:n}=new MessageChannel;r.onmessage=t=>{let{data:r}=t;return e(null!==r)},n.postMessage(t,[t])}))){const e=n();return{result:e instanceof Promise?await e:e}}return{result:!1}}}),l=function(e,t){const r=c(l,t,arguments.length>2&&void 0!==arguments[2]?arguments[2]:()=>!0),n=a(e,r);return e.addEventListener("message",n),()=>e.removeEventListener("message",n)};l(self,{allocate:e=>{let{length:t}=e;const r=(e=>new ArrayBuffer(e))(t);return{result:r,transferables:[r]}},deallocate:()=>({result:void 0})})})()})();`; // tslint:disable-line:max-line-length //# sourceMappingURL=worker.js.map