"use strict";let window={};self.importScripts(self.name);let MsgWorker=window.MsgWorker,socket=null,ports=[],characterPorts=[],initSocket=t=>{let e=new MsgWorker("ws:open");null===socket?((socket=new WebSocket(t)).onopen=(t=>{e.meta({readyState:socket.readyState}),sendToCurrentPort(e)}),socket.onmessage=(t=>{let e=JSON.parse(t.data),r=new MsgWorker("ws:send");r.task(e.task),r.meta({readyState:this.readyState,characterIds:e.characterIds}),r.data(e.load),broadcastPorts(r)}),socket.onclose=(t=>{let e=new MsgWorker("ws:closed");e.meta({readyState:socket.readyState,code:t.code,reason:t.reason,wasClean:t.wasClean}),broadcastPorts(e),socket=null}),socket.onerror=(t=>{let e=new MsgWorker("ws:error");e.meta({readyState:socket.readyState}),sendToCurrentPort(e)})):(e.meta({readyState:socket.readyState}),sendToCurrentPort(e))},sendToCurrentPort=t=>{ports[ports.length-1].postMessage(t)},broadcastPorts=t=>{let e=ports,r=t.meta();r&&r.characterIds&&"undefined"!==r.characterIds&&r.characterIds instanceof Array&&(e=getPortsByCharacterIds(r.characterIds));for(let r=0;r<e.length;r++)e[r].postMessage(t)},addPort=(t,e)=>{(e=parseInt(e))>0?characterPorts.push({characterId:e,port:t}):ports.push(t)},getPortsByCharacterIds=t=>{let e=[];for(let r=0;r<characterPorts.length;r++)for(let a=0;a<t.length;a++)characterPorts[r].characterId===t[a]&&e.push(characterPorts[r].port);return e},removePort=t=>{let e=[],r=characterPorts.length;for(;r--;)characterPorts[r].port===t&&(e.push(characterPorts[r].characterId),characterPorts.splice(r,1));let a=ports.length;for(;a--;)ports[a]===t&&ports.splice(a,1);return[...new Set(e)]};self.addEventListener("connect",t=>{let e=t.ports[0];addPort(e),e.addEventListener("message",t=>{let r=t.data;switch(Object.setPrototypeOf(r,MsgWorker.prototype),r.command){case"ws:init":let t=r.data();addPort(e,t.characterId),initSocket(t.uri);break;case"ws:send":socket.send(JSON.stringify({task:r.task(),load:r.data()}));break;case"sw:closePort":e.close();let a=removePort(e);getPortsByCharacterIds(a).length||socket.send(JSON.stringify({task:r.task(),load:a}))}},!1),e.start()},!1);
//# sourceMappingURL=map.js.map
