import{x as i}from"./jwt-decode.esm-Da8x-1xB.js";(function(){try{var s=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},e=new Error().stack;e&&(s._sentryDebugIds=s._sentryDebugIds||{},s._sentryDebugIds[e]="843035ba-8bfe-43ca-a7a7-d14120baabf2",s._sentryDebugIdIdentifier="sentry-dbid-843035ba-8bfe-43ca-a7a7-d14120baabf2")}catch{}})();class n{static instances=new Map;ws=null;reconnectTimer;keepaliveTimer;messageHandlers=new Set;hasConnectedBefore=!1;url;parse;keepaliveInterval;reconnectDelay;onReconnect;constructor(e){this.url=e.url,this.parse=e.parse,this.keepaliveInterval=e.keepaliveInterval??3e4,this.reconnectDelay=e.reconnectDelay??1e3,this.onReconnect=e.onReconnect,this.connect()}static getInstance(e){const t=n.instances.get(e.url);if(t)return t;const r=new n(e);return n.instances.set(e.url,r),r}handleMessage(e){try{const t=JSON.parse(e.data),r=this.parse?this.parse(t):t;this.messageHandlers.forEach(a=>{try{Promise.resolve(a(r)).catch(i)}catch(c){i(c)}})}catch(t){i(t)}}handleClose(){this.clearTimers(),this.scheduleReconnect()}handleError(){this.clearTimers();try{this.ws?.close()}catch{}this.ws=null,this.scheduleReconnect()}clearTimers(){this.keepaliveTimer&&(clearInterval(this.keepaliveTimer),this.keepaliveTimer=void 0),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=void 0)}scheduleReconnect(){this.reconnectTimer||(this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=void 0,this.connect()},this.reconnectDelay))}startKeepalive(){this.keepaliveTimer=setInterval(()=>{if(!this.ws||this.ws.readyState!==WebSocket.OPEN){this.handleError();return}try{this.ws.send(JSON.stringify({type:"keepalive"}))}catch{this.handleError()}},this.keepaliveInterval)}connect(){if(!(this.ws?.readyState===WebSocket.OPEN||this.ws?.readyState===WebSocket.CONNECTING))try{const e=new WebSocket(this.url);this.ws=e,e.onopen=()=>{this.ws===e&&(this.startKeepalive(),this.hasConnectedBefore&&this.onReconnect?.(),this.hasConnectedBefore=!0)},e.onclose=()=>{this.ws===e&&this.handleClose()},e.onerror=()=>{this.ws===e&&this.handleError()},e.onmessage=t=>{this.ws===e&&this.handleMessage(t)}}catch{this.scheduleReconnect()}}subscribe(e){return this.messageHandlers.add(e),()=>this.messageHandlers.delete(e)}isConnected(){return this.ws?.readyState===WebSocket.OPEN}}export{n as W};
//# sourceMappingURL=webSocketService-DgPUjI2c.js.map
