UNPKG

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