@byterygon/workerr
Version:
Workerr is a lightweight library that simplifies working with Web Workers in JavaScript. It provides a more intuitive way to handle parallel tasks by using Promises, along with a built-in worker pool to efficiently manage worker instances. With strong Typ
2 lines (1 loc) • 21.9 kB
JavaScript
(function(m,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(m=typeof globalThis<"u"?globalThis:m||self,p(m["@byterygon/workerr"]={}))})(this,function(m){"use strict";var Qe=Object.defineProperty;var Ee=m=>{throw TypeError(m)};var Be=(m,p,L)=>p in m?Qe(m,p,{enumerable:!0,configurable:!0,writable:!0,value:L}):m[p]=L;var I=(m,p,L)=>Be(m,typeof p!="symbol"?p+"":p,L),ne=(m,p,L)=>p.has(m)||Ee("Cannot "+L);var u=(m,p,L)=>(ne(m,p,"read from private field"),L?L.call(m):p.get(m)),_=(m,p,L)=>p.has(m)?Ee("Cannot add the same private member more than once"):p instanceof WeakSet?p.add(m):p.set(m,L),E=(m,p,L,N)=>(ne(m,p,"write to private field"),N?N.call(m,L):p.set(m,L),L),P=(m,p,L)=>(ne(m,p,"access private method"),L);var G=(m,p,L,N)=>({set _(S){E(m,p,S,L)},get _(){return u(m,p,N)}});var k,q,$,z,U,W,Q,C,D,T,B,O,H,A,K,y,xe,_e,Pe,Te,Ie,X,se,ie,Z,Ce,ee;class p{constructor(){}buildInvokeHandlers(t){return t}}function L(n){return n&&n.__esModule&&Object.prototype.hasOwnProperty.call(n,"default")?n.default:n}var N={exports:{}},S=typeof Reflect=="object"?Reflect:null,oe=S&&typeof S.apply=="function"?S.apply:function(t,e,r){return Function.prototype.apply.call(t,e,r)},Y;S&&typeof S.ownKeys=="function"?Y=S.ownKeys:Object.getOwnPropertySymbols?Y=function(t){return Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t))}:Y=function(t){return Object.getOwnPropertyNames(t)};function Oe(n){console&&console.warn&&console.warn(n)}var ae=Number.isNaN||function(t){return t!==t};function g(){g.init.call(this)}N.exports=g,N.exports.once=Ne,g.EventEmitter=g,g.prototype._events=void 0,g.prototype._eventsCount=0,g.prototype._maxListeners=void 0;var ue=10;function J(n){if(typeof n!="function")throw new TypeError('The "listener" argument must be of type Function. Received type '+typeof n)}Object.defineProperty(g,"defaultMaxListeners",{enumerable:!0,get:function(){return ue},set:function(n){if(typeof n!="number"||n<0||ae(n))throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+n+".");ue=n}}),g.init=function(){(this._events===void 0||this._events===Object.getPrototypeOf(this)._events)&&(this._events=Object.create(null),this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},g.prototype.setMaxListeners=function(t){if(typeof t!="number"||t<0||ae(t))throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received '+t+".");return this._maxListeners=t,this};function ce(n){return n._maxListeners===void 0?g.defaultMaxListeners:n._maxListeners}g.prototype.getMaxListeners=function(){return ce(this)},g.prototype.emit=function(t){for(var e=[],r=1;r<arguments.length;r++)e.push(arguments[r]);var s=t==="error",o=this._events;if(o!==void 0)s=s&&o.error===void 0;else if(!s)return!1;if(s){var i;if(e.length>0&&(i=e[0]),i instanceof Error)throw i;var a=new Error("Unhandled error."+(i?" ("+i.message+")":""));throw a.context=i,a}var h=o[t];if(h===void 0)return!1;if(typeof h=="function")oe(h,this,e);else for(var c=h.length,l=me(h,c),r=0;r<c;++r)oe(l[r],this,e);return!0};function le(n,t,e,r){var s,o,i;if(J(e),o=n._events,o===void 0?(o=n._events=Object.create(null),n._eventsCount=0):(o.newListener!==void 0&&(n.emit("newListener",t,e.listener?e.listener:e),o=n._events),i=o[t]),i===void 0)i=o[t]=e,++n._eventsCount;else if(typeof i=="function"?i=o[t]=r?[e,i]:[i,e]:r?i.unshift(e):i.push(e),s=ce(n),s>0&&i.length>s&&!i.warned){i.warned=!0;var a=new Error("Possible EventEmitter memory leak detected. "+i.length+" "+String(t)+" listeners added. Use emitter.setMaxListeners() to increase limit");a.name="MaxListenersExceededWarning",a.emitter=n,a.type=t,a.count=i.length,Oe(a)}return n}g.prototype.addListener=function(t,e){return le(this,t,e,!1)},g.prototype.on=g.prototype.addListener,g.prototype.prependListener=function(t,e){return le(this,t,e,!0)};function ke(){if(!this.fired)return this.target.removeListener(this.type,this.wrapFn),this.fired=!0,arguments.length===0?this.listener.call(this.target):this.listener.apply(this.target,arguments)}function fe(n,t,e){var r={fired:!1,wrapFn:void 0,target:n,type:t,listener:e},s=ke.bind(r);return s.listener=e,r.wrapFn=s,s}g.prototype.once=function(t,e){return J(e),this.on(t,fe(this,t,e)),this},g.prototype.prependOnceListener=function(t,e){return J(e),this.prependListener(t,fe(this,t,e)),this},g.prototype.removeListener=function(t,e){var r,s,o,i,a;if(J(e),s=this._events,s===void 0)return this;if(r=s[t],r===void 0)return this;if(r===e||r.listener===e)--this._eventsCount===0?this._events=Object.create(null):(delete s[t],s.removeListener&&this.emit("removeListener",t,r.listener||e));else if(typeof r!="function"){for(o=-1,i=r.length-1;i>=0;i--)if(r[i]===e||r[i].listener===e){a=r[i].listener,o=i;break}if(o<0)return this;o===0?r.shift():Ae(r,o),r.length===1&&(s[t]=r[0]),s.removeListener!==void 0&&this.emit("removeListener",t,a||e)}return this},g.prototype.off=g.prototype.removeListener,g.prototype.removeAllListeners=function(t){var e,r,s;if(r=this._events,r===void 0)return this;if(r.removeListener===void 0)return arguments.length===0?(this._events=Object.create(null),this._eventsCount=0):r[t]!==void 0&&(--this._eventsCount===0?this._events=Object.create(null):delete r[t]),this;if(arguments.length===0){var o=Object.keys(r),i;for(s=0;s<o.length;++s)i=o[s],i!=="removeListener"&&this.removeAllListeners(i);return this.removeAllListeners("removeListener"),this._events=Object.create(null),this._eventsCount=0,this}if(e=r[t],typeof e=="function")this.removeListener(t,e);else if(e!==void 0)for(s=e.length-1;s>=0;s--)this.removeListener(t,e[s]);return this};function he(n,t,e){var r=n._events;if(r===void 0)return[];var s=r[t];return s===void 0?[]:typeof s=="function"?e?[s.listener||s]:[s]:e?Me(s):me(s,s.length)}g.prototype.listeners=function(t){return he(this,t,!0)},g.prototype.rawListeners=function(t){return he(this,t,!1)},g.listenerCount=function(n,t){return typeof n.listenerCount=="function"?n.listenerCount(t):de.call(n,t)},g.prototype.listenerCount=de;function de(n){var t=this._events;if(t!==void 0){var e=t[n];if(typeof e=="function")return 1;if(e!==void 0)return e.length}return 0}g.prototype.eventNames=function(){return this._eventsCount>0?Y(this._events):[]};function me(n,t){for(var e=new Array(t),r=0;r<t;++r)e[r]=n[r];return e}function Ae(n,t){for(;t+1<n.length;t++)n[t]=n[t+1];n.pop()}function Me(n){for(var t=new Array(n.length),e=0;e<t.length;++e)t[e]=n[e].listener||n[e];return t}function Ne(n,t){return new Promise(function(e,r){function s(i){n.removeListener(t,o),r(i)}function o(){typeof n.removeListener=="function"&&n.removeListener("error",s),e([].slice.call(arguments))}ve(n,t,o,{once:!0}),t!=="error"&&Se(n,s,{once:!0})})}function Se(n,t,e){typeof n.on=="function"&&ve(n,"error",t,e)}function ve(n,t,e,r){if(typeof n.on=="function")r.once?n.once(t,e):n.on(t,e);else if(typeof n.addEventListener=="function")n.addEventListener(t,function s(o){r.once&&n.removeEventListener(t,s),e(o)});else throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof n)}var ze=N.exports;const ye=L(ze);function te(){const n=URL.createObjectURL(new Blob),t=n.toString();return URL.revokeObjectURL(n),t.substr(t.lastIndexOf("/")+1)}function pe(n){if(n instanceof Error)return n;if(typeof n=="string")return new Error(n);if(typeof n=="object"&&n!==null)try{const t=JSON.stringify(n,Re());return new Error(t)}catch{return new Error("An error occurred, but it could not be stringified.")}else return new Error(String(n))}function Re(){const n=new WeakSet;return(t,e)=>{if(typeof e=="object"&&e!==null){if(n.has(e))return"[Circular]";n.add(e)}return e}}class F{constructor({invokeHandler:t}){I(this,"context");I(this,"invokeHandler");I(this,"emitter",new ye);this.invokeHandler=t,this.context={};const e=async r=>{var s;switch(r.data.messageType){case"context:update":this.context=r.data.messagePayload,F.postMessage({messageType:"context:ack",messagePayload:{messageId:r.data.messageId}}),this.emitter.emit("context:update",this.context);break;case"excecute:start":{const o=r.data;try{if(o.messagePayload.cmd in this.invokeHandler){let i;if(o.messagePayload.abortSignalChannelPort){const c=new AbortController;i=c.signal,o.messagePayload.abortSignalChannelPort.onmessage=l=>{var f;c.abort(l.data),(f=o.messagePayload.abortSignalChannelPort)==null||f.close()}}let a=[];const h=await this.invokeHandler[o.messagePayload.cmd](o.messagePayload.params,{context:this.context,abortSignal:i,transferObject(c){a.push(...c)}});(s=o.messagePayload.abortSignalChannelPort)==null||s.close(),F.postMessage({messageType:"excecute:response",messagePayload:{messageId:o.messageId,result:h}},a)}else throw new Error("Command is not supported")}catch(i){const a=pe(i);F.postMessage({messageType:"excecute:error",messagePayload:{messageId:r.data.messageId,error:a}})}}}};self.addEventListener("message",e)}static async create(t){this.postMessage({messageType:"initialization:start",messagePayload:void 0});try{const{invokeHandler:e}=await t();return await new Promise((r,s)=>{const o=a=>{a.data.messageType==="initialization:ack"&&(r(void 0),self.removeEventListener("messageerror",i),self.removeEventListener("message",o))},i=a=>{self.removeEventListener("messageerror",i),self.removeEventListener("message",o),console.error("Unable to deserialized message:",a.data),s(new Error("Unable to deserialized message"))};self.addEventListener("message",o),self.addEventListener("messageerror",i),this.postMessage({messageType:"initialization:complete",messagePayload:void 0})}),new F({invokeHandler:e})}catch(e){this.postMessage({messageType:"initialization:error",messagePayload:pe(e)})}}static postMessage(t,e){self.postMessage({...t,messageId:t.messageId??te(),timestamp:t.timestamp??Date.now()},e)}addListener(t,e){this.emitter.addListener(t,e)}removeListener(t,e){this.emitter.removeListener(t,e)}removeAllListeners(t){this.emitter.removeAllListeners(t)}}var ge={exports:{}};(function(n){var t=Object.prototype.hasOwnProperty,e="~";function r(){}Object.create&&(r.prototype=Object.create(null),new r().__proto__||(e=!1));function s(h,c,l){this.fn=h,this.context=c,this.once=l||!1}function o(h,c,l,f,w){if(typeof l!="function")throw new TypeError("The listener must be a function");var v=new s(l,f||h,w),b=e?e+c:c;return h._events[b]?h._events[b].fn?h._events[b]=[h._events[b],v]:h._events[b].push(v):(h._events[b]=v,h._eventsCount++),h}function i(h,c){--h._eventsCount===0?h._events=new r:delete h._events[c]}function a(){this._events=new r,this._eventsCount=0}a.prototype.eventNames=function(){var c=[],l,f;if(this._eventsCount===0)return c;for(f in l=this._events)t.call(l,f)&&c.push(e?f.slice(1):f);return Object.getOwnPropertySymbols?c.concat(Object.getOwnPropertySymbols(l)):c},a.prototype.listeners=function(c){var l=e?e+c:c,f=this._events[l];if(!f)return[];if(f.fn)return[f.fn];for(var w=0,v=f.length,b=new Array(v);w<v;w++)b[w]=f[w].fn;return b},a.prototype.listenerCount=function(c){var l=e?e+c:c,f=this._events[l];return f?f.fn?1:f.length:0},a.prototype.emit=function(c,l,f,w,v,b){var M=e?e+c:c;if(!this._events[M])return!1;var d=this._events[M],R=arguments.length,j,x;if(d.fn){switch(d.once&&this.removeListener(c,d.fn,void 0,!0),R){case 1:return d.fn.call(d.context),!0;case 2:return d.fn.call(d.context,l),!0;case 3:return d.fn.call(d.context,l,f),!0;case 4:return d.fn.call(d.context,l,f,w),!0;case 5:return d.fn.call(d.context,l,f,w,v),!0;case 6:return d.fn.call(d.context,l,f,w,v,b),!0}for(x=1,j=new Array(R-1);x<R;x++)j[x-1]=arguments[x];d.fn.apply(d.context,j)}else{var Ue=d.length,V;for(x=0;x<Ue;x++)switch(d[x].once&&this.removeListener(c,d[x].fn,void 0,!0),R){case 1:d[x].fn.call(d[x].context);break;case 2:d[x].fn.call(d[x].context,l);break;case 3:d[x].fn.call(d[x].context,l,f);break;case 4:d[x].fn.call(d[x].context,l,f,w);break;default:if(!j)for(V=1,j=new Array(R-1);V<R;V++)j[V-1]=arguments[V];d[x].fn.apply(d[x].context,j)}}return!0},a.prototype.on=function(c,l,f){return o(this,c,l,f,!1)},a.prototype.once=function(c,l,f){return o(this,c,l,f,!0)},a.prototype.removeListener=function(c,l,f,w){var v=e?e+c:c;if(!this._events[v])return this;if(!l)return i(this,v),this;var b=this._events[v];if(b.fn)b.fn===l&&(!w||b.once)&&(!f||b.context===f)&&i(this,v);else{for(var M=0,d=[],R=b.length;M<R;M++)(b[M].fn!==l||w&&!b[M].once||f&&b[M].context!==f)&&d.push(b[M]);d.length?this._events[v]=d.length===1?d[0]:d:i(this,v)}return this},a.prototype.removeAllListeners=function(c){var l;return c?(l=e?e+c:c,this._events[l]&&i(this,l)):(this._events=new r,this._eventsCount=0),this},a.prototype.off=a.prototype.removeListener,a.prototype.addListener=a.prototype.on,a.prefixed=e,a.EventEmitter=a,n.exports=a})(ge);var je=ge.exports;const Fe=L(je);class we extends Error{constructor(t){super(t),this.name="TimeoutError"}}class qe extends Error{constructor(t){super(),this.name="AbortError",this.message=t}}const be=n=>globalThis.DOMException===void 0?new qe(n):new DOMException(n),Le=n=>{const t=n.reason===void 0?be("This operation was aborted."):n.reason;return t instanceof Error?t:be(t)};function $e(n,t){const{milliseconds:e,fallback:r,message:s,customTimers:o={setTimeout,clearTimeout}}=t;let i,a;const c=new Promise((l,f)=>{if(typeof e!="number"||Math.sign(e)!==1)throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${e}\``);if(t.signal){const{signal:v}=t;v.aborted&&f(Le(v)),a=()=>{f(Le(v))},v.addEventListener("abort",a,{once:!0})}if(e===Number.POSITIVE_INFINITY){n.then(l,f);return}const w=new we;i=o.setTimeout.call(void 0,()=>{if(r){try{l(r())}catch(v){f(v)}return}typeof n.cancel=="function"&&n.cancel(),s===!1?l():s instanceof Error?f(s):(w.message=s??`Promise timed out after ${e} milliseconds`,f(w))},e),(async()=>{try{l(await n)}catch(v){f(v)}})()}).finally(()=>{c.clear(),a&&t.signal&&t.signal.removeEventListener("abort",a)});return c.clear=()=>{o.clearTimeout.call(void 0,i),i=void 0},c}function We(n,t,e){let r=0,s=n.length;for(;s>0;){const o=Math.trunc(s/2);let i=r+o;e(n[i],t)<=0?(r=++i,s-=o+1):s=o}return r}class De{constructor(){_(this,k,[])}enqueue(t,e){e={priority:0,...e};const r={priority:e.priority,run:t};if(this.size&&u(this,k)[this.size-1].priority>=e.priority){u(this,k).push(r);return}const s=We(u(this,k),r,(o,i)=>i.priority-o.priority);u(this,k).splice(s,0,r)}dequeue(){const t=u(this,k).shift();return t==null?void 0:t.run}filter(t){return u(this,k).filter(e=>e.priority===t.priority).map(e=>e.run)}get size(){return u(this,k).length}}k=new WeakMap;class He extends Fe{constructor(e){var r,s;super();_(this,y);_(this,q);_(this,$);_(this,z,0);_(this,U);_(this,W);_(this,Q,0);_(this,C);_(this,D);_(this,T);_(this,B);_(this,O,0);_(this,H);_(this,A);_(this,K);I(this,"timeout");if(e={carryoverConcurrencyCount:!1,intervalCap:Number.POSITIVE_INFINITY,interval:0,concurrency:Number.POSITIVE_INFINITY,autoStart:!0,queueClass:De,...e},!(typeof e.intervalCap=="number"&&e.intervalCap>=1))throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${((r=e.intervalCap)==null?void 0:r.toString())??""}\` (${typeof e.intervalCap})`);if(e.interval===void 0||!(Number.isFinite(e.interval)&&e.interval>=0))throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${((s=e.interval)==null?void 0:s.toString())??""}\` (${typeof e.interval})`);E(this,q,e.carryoverConcurrencyCount),E(this,$,e.intervalCap===Number.POSITIVE_INFINITY||e.interval===0),E(this,U,e.intervalCap),E(this,W,e.interval),E(this,T,new e.queueClass),E(this,B,e.queueClass),this.concurrency=e.concurrency,this.timeout=e.timeout,E(this,K,e.throwOnTimeout===!0),E(this,A,e.autoStart===!1)}get concurrency(){return u(this,H)}set concurrency(e){if(!(typeof e=="number"&&e>=1))throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${e}\` (${typeof e})`);E(this,H,e),P(this,y,Z).call(this)}async add(e,r={}){return r={timeout:this.timeout,throwOnTimeout:u(this,K),...r},new Promise((s,o)=>{u(this,T).enqueue(async()=>{var i;G(this,O)._++,G(this,z)._++;try{(i=r.signal)==null||i.throwIfAborted();let a=e({signal:r.signal});r.timeout&&(a=$e(Promise.resolve(a),{milliseconds:r.timeout})),r.signal&&(a=Promise.race([a,P(this,y,Ce).call(this,r.signal)]));const h=await a;s(h),this.emit("completed",h)}catch(a){if(a instanceof we&&!r.throwOnTimeout){s();return}o(a),this.emit("error",a)}finally{P(this,y,Pe).call(this)}},r),this.emit("add"),P(this,y,X).call(this)})}async addAll(e,r){return Promise.all(e.map(async s=>this.add(s,r)))}start(){return u(this,A)?(E(this,A,!1),P(this,y,Z).call(this),this):this}pause(){E(this,A,!0)}clear(){E(this,T,new(u(this,B)))}async onEmpty(){u(this,T).size!==0&&await P(this,y,ee).call(this,"empty")}async onSizeLessThan(e){u(this,T).size<e||await P(this,y,ee).call(this,"next",()=>u(this,T).size<e)}async onIdle(){u(this,O)===0&&u(this,T).size===0||await P(this,y,ee).call(this,"idle")}get size(){return u(this,T).size}sizeBy(e){return u(this,T).filter(e).length}get pending(){return u(this,O)}get isPaused(){return u(this,A)}}q=new WeakMap,$=new WeakMap,z=new WeakMap,U=new WeakMap,W=new WeakMap,Q=new WeakMap,C=new WeakMap,D=new WeakMap,T=new WeakMap,B=new WeakMap,O=new WeakMap,H=new WeakMap,A=new WeakMap,K=new WeakMap,y=new WeakSet,xe=function(){return u(this,$)||u(this,z)<u(this,U)},_e=function(){return u(this,O)<u(this,H)},Pe=function(){G(this,O)._--,P(this,y,X).call(this),this.emit("next")},Te=function(){P(this,y,ie).call(this),P(this,y,se).call(this),E(this,D,void 0)},Ie=function(){const e=Date.now();if(u(this,C)===void 0){const r=u(this,Q)-e;if(r<0)E(this,z,u(this,q)?u(this,O):0);else return u(this,D)===void 0&&E(this,D,setTimeout(()=>{P(this,y,Te).call(this)},r)),!0}return!1},X=function(){if(u(this,T).size===0)return u(this,C)&&clearInterval(u(this,C)),E(this,C,void 0),this.emit("empty"),u(this,O)===0&&this.emit("idle"),!1;if(!u(this,A)){const e=!u(this,y,Ie);if(u(this,y,xe)&&u(this,y,_e)){const r=u(this,T).dequeue();return r?(this.emit("active"),r(),e&&P(this,y,se).call(this),!0):!1}}return!1},se=function(){u(this,$)||u(this,C)!==void 0||(E(this,C,setInterval(()=>{P(this,y,ie).call(this)},u(this,W))),E(this,Q,Date.now()+u(this,W)))},ie=function(){u(this,z)===0&&u(this,O)===0&&u(this,C)&&(clearInterval(u(this,C)),E(this,C,void 0)),E(this,z,u(this,q)?u(this,O):0),P(this,y,Z).call(this)},Z=function(){for(;P(this,y,X).call(this););},Ce=async function(e){return new Promise((r,s)=>{e.addEventListener("abort",()=>{s(e.reason)},{once:!0})})},ee=async function(e,r){return new Promise(s=>{const o=()=>{r&&!r()||(this.off(e,o),s())};this.on(e,o)})};class re{constructor(t){I(this,"worker");I(this,"taskQueue");I(this,"concurrency");I(this,"context");I(this,"eventEmitter",new ye);I(this,"readyPromise");I(this,"ready",!1);I(this,"terminated",!1);try{this.worker=t.worker(),this.context=t.context??{},this.concurrency=t.concurrency||1/0,Number.isFinite(this.concurrency)&&this.concurrency>=1&&(this.taskQueue=new He({concurrency:this.concurrency})),this.readyPromise=new Promise((e,r)=>{const s=setTimeout(()=>{r(new Error("Cannot initialized worker"))},5e3),o=i=>{switch(i.data.messageType){case"initialization:start":clearTimeout(s);break;case"initialization:error":this.eventEmitter.emit("error",i.data.messagePayload),r(i.data.messagePayload),this.worker.removeEventListener("message",o);break;case"initialization:complete":this.ready=!0,this.postMessage({messageType:"initialization:ack",messagePayload:void 0}),this.updateContext(()=>this.context),this.worker.removeEventListener("message",o),this.eventEmitter.emit("ready"),e(!0);break}};this.worker.addEventListener("message",o)})}catch(e){throw e}}static create(t){return new re(t)}postMessage(t,e){this.worker.postMessage({...t,messageId:t.messageId??te(),timestamp:t.timestamp??Date.now()},e)}async awaitReady(){await this.readyPromise}async updateContext(t){if(this.terminated)throw new Error("Workerr had been terminated");this.context=t(this.context),this.postMessage({messageType:"context:update",messagePayload:this.context})}_invoke(t,e,r){return new Promise(async(s,o)=>{var f;await this.awaitReady;const i=te();let a,h;if(r!=null&&r.abortSignal){const w=new MessageChannel;a=w.port1,h=w.port2}const c=w=>{const v=w.target;a==null||a.postMessage(v.reason)};(f=r==null?void 0:r.abortSignal)==null||f.addEventListener("abort",c,{once:!0});const l=w=>{var b;let v=w.data;if((v.messageType==="excecute:error"||v.messageType==="excecute:response")&&v.messagePayload.messageId===i){switch(v.messageType){case"excecute:response":this.worker.removeEventListener("message",l),s(v.messagePayload.result);break;case"excecute:error":{this.worker.removeEventListener("message",l),o(v.messagePayload.error);break}}(b=r==null?void 0:r.abortSignal)==null||b.removeEventListener("abort",c),a==null||a.close()}};this.worker.addEventListener("message",l),this.postMessage({messageType:"excecute:start",messagePayload:{cmd:t,params:e,abortSignalChannelPort:h},messageId:i},{transfer:[...(r==null?void 0:r.transfer)??[],...h?[h]:[]]})})}async invoke(t,e,r){if(this.terminated)throw new Error("Workerr had been terminated");return this.taskQueue?this.taskQueue.add(({signal:s})=>this._invoke(t,e,{...r,abortSignal:s}),{signal:r==null?void 0:r.abortSignal}):this._invoke(t,e,r)}addListener(t,e){if(this.terminated)throw new Error("Workerr had been terminated");this.eventEmitter.addListener(t,e)}removeListener(t,e){if(this.terminated)throw new Error("Workerr had been terminated");this.eventEmitter.removeListener(t,e)}removeAllListeners(t){if(this.terminated)throw new Error("Workerr had been terminated");this.eventEmitter.removeAllListeners(t)}terminate(){this.worker.terminate(),this.removeAllListeners()}}m.CommandBuilder=p,m.Workerr=F,m.WorkerrController=re,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})});