async-array-buffer
Version:
This module allows to allocate and deallocate ArrayBuffers without blocking the main thread.
53 lines (47 loc) • 15.5 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('async-array-buffer-broker')) :
typeof define === 'function' && define.amd ? define(['exports', 'async-array-buffer-broker'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.asyncArrayBuffer = {}, global.asyncArrayBufferBroker));
})(this, (function (exports, asyncArrayBufferBroker) { 'use strict';
var createLoadOrReturnBroker = function createLoadOrReturnBroker(loadBroker, worker) {
var broker = null;
return function () {
if (broker !== null) {
return broker;
}
var blob = new Blob([worker], {
type: 'application/javascript; charset=utf-8'
});
var url = URL.createObjectURL(blob);
broker = loadBroker(url);
// Bug #1: Edge up until v18 didn't like the URL to be revoked directly.
setTimeout(function () {
return URL.revokeObjectURL(url);
});
return broker;
};
};
// This is the minified and stringified code of the async-array-buffer-worker package.
var worker = "(()=>{var t={932:function(t,e,r){!function(t){\"use strict\";var e=function(t){return new ArrayBuffer(t)};t.createWorker(self,{allocate:function(t){var r=t.length,n=e(r);return{result:n,transferables:[n]}},deallocate:function(){return{result:void 0}}})}(r(623))},389:function(t,e){!function(t){\"use strict\";var e=function(t){return function(e){var r=t(e);return e.add(r),r}},r=function(t){return function(e,r){return t.set(e,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,i=2*o,a=function(t,e){return function(r){var a=e.get(r),u=void 0===a?r.size:a<i?a+1:0;if(!r.has(u))return t(r,u);if(r.size<o){for(;r.has(u);)u=Math.floor(Math.random()*i);return t(r,u)}if(r.size>n)throw new Error(\"Congratulations, you created a collection of unique numbers which uses all available integers!\");for(;r.has(u);)u=Math.floor(Math.random()*n);return t(r,u)}},u=new WeakMap,c=r(u),s=a(c,u),f=e(s);t.addUniqueNumber=f,t.generateUniqueNumber=s}(e)},623:function(t,e,r){!function(t,e,r,n,o){\"use strict\";var i={INTERNAL_ERROR:-32603,INVALID_PARAMS:-32602,METHOD_NOT_FOUND:-32601},a=function(t,e){return Object.assign(new Error(t),{status:e})},u=function(t){return a('The requested method called \"'.concat(t,'\" is not supported.'),i.METHOD_NOT_FOUND)},c=function(t){return a('The handler of the method called \"'.concat(t,'\" returned no required result.'),i.INTERNAL_ERROR)},s=function(t){return a('The handler of the method called \"'.concat(t,'\" returned an unexpected result.'),i.INTERNAL_ERROR)},f=function(t){return a('The specified parameter called \"portId\" with the given value \"'.concat(t,'\" does not identify a port connected to this worker.'),i.INVALID_PARAMS)},l=function(t,n){return function(){var o=e(r.mark((function e(o){var i,a,f,l,p,h,d,v,y,g,m,b,x;return r.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(i=o.data,a=i.id,f=i.method,l=i.params,p=n[f],e.prev=2,void 0!==p){e.next=5;break}throw u(f);case 5:if(void 0!==(h=void 0===l?p():p(l))){e.next=8;break}throw c(f);case 8:if(!(h instanceof Promise)){e.next=14;break}return e.next=11,h;case 11:e.t0=e.sent,e.next=15;break;case 14:e.t0=h;case 15:if(d=e.t0,null!==a){e.next=21;break}if(void 0===d.result){e.next=19;break}throw s(f);case 19:e.next=25;break;case 21:if(void 0!==d.result){e.next=23;break}throw s(f);case 23:v=d.result,y=d.transferables,g=void 0===y?[]:y,t.postMessage({id:a,result:v},g);case 25:e.next=31;break;case 27:e.prev=27,e.t1=e.catch(2),m=e.t1.message,b=e.t1.status,x=void 0===b?-32603:b,t.postMessage({error:{code:x,message:m},id:a});case 31:case\"end\":return e.stop()}}),e,null,[[2,27]])})));return function(t){return o.apply(this,arguments)}}()},p=function(){return new Promise((function(t){var e=new ArrayBuffer(0),r=new MessageChannel,n=r.port1,o=r.port2;n.onmessage=function(e){var r=e.data;return t(null!==r)},o.postMessage(e,[e])}))};function h(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function d(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?h(Object(r),!0).forEach((function(e){n(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):h(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var v=new Map,y=function(t,n,i){return d(d({},n),{},{connect:function(e){var r=e.port;r.start();var i=t(r,n),a=o.generateUniqueNumber(v);return v.set(a,(function(){i(),r.close(),v.delete(a)})),{result:a}},disconnect:function(t){var e=t.portId,r=v.get(e);if(void 0===r)throw f(e);return r(),{result:null}},isSupported:function(){var t=e(r.mark((function t(){var e,n;return r.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,p();case 2:if(!t.sent){t.next=14;break}if(!((e=i())instanceof Promise)){t.next=11;break}return t.next=8,e;case 8:t.t0=t.sent,t.next=12;break;case 11:t.t0=e;case 12:return n=t.t0,t.abrupt(\"return\",{result:n});case 14:return t.abrupt(\"return\",{result:!1});case 15:case\"end\":return t.stop()}}),t)})));function n(){return t.apply(this,arguments)}return n}()})},g=function t(e,r){var n=y(t,r,arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(){return!0}),o=l(e,n);return e.addEventListener(\"message\",o),function(){return e.removeEventListener(\"message\",o)}};t.createWorker=g,t.isSupported=p}(e,r(293),r(756),r(693),r(389))},293:t=>{function e(t,e,r,n,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}t.exports=function(t){return function(){var r=this,n=arguments;return new Promise((function(o,i){var a=t.apply(r,n);function u(t){e(a,o,i,u,c,\"next\",t)}function c(t){e(a,o,i,u,c,\"throw\",t)}u(void 0)}))}},t.exports.__esModule=!0,t.exports.default=t.exports},693:(t,e,r)=>{var n=r(736);t.exports=function(t,e,r){return(e=n(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t},t.exports.__esModule=!0,t.exports.default=t.exports},633:(t,e,r)=>{var n=r(738).default;function o(){\"use strict\";t.exports=o=function(){return r},t.exports.__esModule=!0,t.exports.default=t.exports;var e,r={},i=Object.prototype,a=i.hasOwnProperty,u=Object.defineProperty||function(t,e,r){t[e]=r.value},c=\"function\"==typeof Symbol?Symbol:{},s=c.iterator||\"@@iterator\",f=c.asyncIterator||\"@@asyncIterator\",l=c.toStringTag||\"@@toStringTag\";function p(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{p({},\"\")}catch(e){p=function(t,e,r){return t[e]=r}}function h(t,e,r,n){var o=e&&e.prototype instanceof x?e:x,i=Object.create(o.prototype),a=new R(n||[]);return u(i,\"_invoke\",{value:P(t,r,a)}),i}function d(t,e,r){try{return{type:\"normal\",arg:t.call(e,r)}}catch(t){return{type:\"throw\",arg:t}}}r.wrap=h;var v=\"suspendedStart\",y=\"suspendedYield\",g=\"executing\",m=\"completed\",b={};function x(){}function w(){}function _(){}var O={};p(O,s,(function(){return this}));var E=Object.getPrototypeOf,L=E&&E(E(A([])));L&&L!==i&&a.call(L,s)&&(O=L);var j=_.prototype=x.prototype=Object.create(O);function N(t){[\"next\",\"throw\",\"return\"].forEach((function(e){p(t,e,(function(t){return this._invoke(e,t)}))}))}function k(t,e){function r(o,i,u,c){var s=d(t[o],t,i);if(\"throw\"!==s.type){var f=s.arg,l=f.value;return l&&\"object\"==n(l)&&a.call(l,\"__await\")?e.resolve(l.__await).then((function(t){r(\"next\",t,u,c)}),(function(t){r(\"throw\",t,u,c)})):e.resolve(l).then((function(t){f.value=t,u(f)}),(function(t){return r(\"throw\",t,u,c)}))}c(s.arg)}var o;u(this,\"_invoke\",{value:function(t,n){function i(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(i,i):i()}})}function P(t,r,n){var o=v;return function(i,a){if(o===g)throw Error(\"Generator is already running\");if(o===m){if(\"throw\"===i)throw a;return{value:e,done:!0}}for(n.method=i,n.arg=a;;){var u=n.delegate;if(u){var c=M(u,n);if(c){if(c===b)continue;return c}}if(\"next\"===n.method)n.sent=n._sent=n.arg;else if(\"throw\"===n.method){if(o===v)throw o=m,n.arg;n.dispatchException(n.arg)}else\"return\"===n.method&&n.abrupt(\"return\",n.arg);o=g;var s=d(t,r,n);if(\"normal\"===s.type){if(o=n.done?m:y,s.arg===b)continue;return{value:s.arg,done:n.done}}\"throw\"===s.type&&(o=m,n.method=\"throw\",n.arg=s.arg)}}}function M(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,\"throw\"===n&&t.iterator.return&&(r.method=\"return\",r.arg=e,M(t,r),\"throw\"===r.method)||\"return\"!==n&&(r.method=\"throw\",r.arg=new TypeError(\"The iterator does not provide a '\"+n+\"' method\")),b;var i=d(o,t.iterator,r.arg);if(\"throw\"===i.type)return r.method=\"throw\",r.arg=i.arg,r.delegate=null,b;var a=i.arg;return a?a.done?(r[t.resultName]=a.value,r.next=t.nextLoc,\"return\"!==r.method&&(r.method=\"next\",r.arg=e),r.delegate=null,b):a:(r.method=\"throw\",r.arg=new TypeError(\"iterator result is not an object\"),r.delegate=null,b)}function T(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function S(t){var e=t.completion||{};e.type=\"normal\",delete e.arg,t.completion=e}function R(t){this.tryEntries=[{tryLoc:\"root\"}],t.forEach(T,this),this.reset(!0)}function A(t){if(t||\"\"===t){var r=t[s];if(r)return r.call(t);if(\"function\"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,i=function r(){for(;++o<t.length;)if(a.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return i.next=i}}throw new TypeError(n(t)+\" is not iterable\")}return w.prototype=_,u(j,\"constructor\",{value:_,configurable:!0}),u(_,\"constructor\",{value:w,configurable:!0}),w.displayName=p(_,l,\"GeneratorFunction\"),r.isGeneratorFunction=function(t){var e=\"function\"==typeof t&&t.constructor;return!!e&&(e===w||\"GeneratorFunction\"===(e.displayName||e.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,_):(t.__proto__=_,p(t,l,\"GeneratorFunction\")),t.prototype=Object.create(j),t},r.awrap=function(t){return{__await:t}},N(k.prototype),p(k.prototype,f,(function(){return this})),r.AsyncIterator=k,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new k(h(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},N(j),p(j,l,\"Generator\"),p(j,s,(function(){return this})),p(j,\"toString\",(function(){return\"[object Generator]\"})),r.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=A,R.prototype={constructor:R,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method=\"next\",this.arg=e,this.tryEntries.forEach(S),!t)for(var r in this)\"t\"===r.charAt(0)&&a.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if(\"throw\"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(n,o){return u.type=\"throw\",u.arg=t,r.next=n,o&&(r.method=\"next\",r.arg=e),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],u=i.completion;if(\"root\"===i.tryLoc)return n(\"end\");if(i.tryLoc<=this.prev){var c=a.call(i,\"catchLoc\"),s=a.call(i,\"finallyLoc\");if(c&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw Error(\"try statement without catch or finally\");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&a.call(n,\"finallyLoc\")&&this.prev<n.finallyLoc){var o=n;break}}o&&(\"break\"===t||\"continue\"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var i=o?o.completion:{};return i.type=t,i.arg=e,o?(this.method=\"next\",this.next=o.finallyLoc,b):this.complete(i)},complete:function(t,e){if(\"throw\"===t.type)throw t.arg;return\"break\"===t.type||\"continue\"===t.type?this.next=t.arg:\"return\"===t.type?(this.rval=this.arg=t.arg,this.method=\"return\",this.next=\"end\"):\"normal\"===t.type&&e&&(this.next=e),b},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),S(r),b}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if(\"throw\"===n.type){var o=n.arg;S(r)}return o}}throw Error(\"illegal catch attempt\")},delegateYield:function(t,r,n){return this.delegate={iterator:A(t),resultName:r,nextLoc:n},\"next\"===this.method&&(this.arg=e),b}},r}t.exports=o,t.exports.__esModule=!0,t.exports.default=t.exports},45:(t,e,r)=>{var n=r(738).default;t.exports=function(t,e){if(\"object\"!=n(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var o=r.call(t,e||\"default\");if(\"object\"!=n(o))return o;throw new TypeError(\"@@toPrimitive must return a primitive value.\")}return(\"string\"===e?String:Number)(t)},t.exports.__esModule=!0,t.exports.default=t.exports},736:(t,e,r)=>{var n=r(738).default,o=r(45);t.exports=function(t){var e=o(t,\"string\");return\"symbol\"==n(e)?e:e+\"\"},t.exports.__esModule=!0,t.exports.default=t.exports},738:t=>{function e(r){return t.exports=e=\"function\"==typeof Symbol&&\"symbol\"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&\"function\"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?\"symbol\":typeof t},t.exports.__esModule=!0,t.exports.default=t.exports,e(r)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports},756:(t,e,r)=>{var n=r(633)();t.exports=n;try{regeneratorRuntime=n}catch(t){\"object\"==typeof globalThis?globalThis.regeneratorRuntime=n:Function(\"r\",\"regeneratorRuntime = r\")(n)}}},e={};function r(n){var o=e[n];if(void 0!==o)return o.exports;var i=e[n]={exports:{}};return t[n].call(i.exports,i,i.exports,r),i.exports}r.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return r.d(e,{a:e}),e},r.d=(t,e)=>{for(var n in e)r.o(e,n)&&!r.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";r(932)})()})();"; // tslint:disable-line:max-line-length
var loadOrReturnBroker = createLoadOrReturnBroker(asyncArrayBufferBroker.load, worker);
var allocate = function allocate(length) {
return loadOrReturnBroker().allocate(length);
};
var connect = function connect() {
return loadOrReturnBroker().connect();
};
var deallocate = function deallocate(arrayBuffer) {
return loadOrReturnBroker().deallocate(arrayBuffer);
};
var disconnect = function disconnect(port) {
return loadOrReturnBroker().disconnect(port);
};
var isSupported = function isSupported() {
return loadOrReturnBroker().isSupported();
};
exports.allocate = allocate;
exports.connect = connect;
exports.deallocate = deallocate;
exports.disconnect = disconnect;
exports.isSupported = isSupported;
}));