import{i as f}from"./contracts.generated-Dr3k2sV9.js";import{bg as c,r as o,K as v,M as m,a6 as l}from"./jwt-decode.esm-C7p7KYmD.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]="42f2dfe4-2e23-486f-b083-f34318ea5df9",s._sentryDebugIdIdentifier="sentry-dbid-42f2dfe4-2e23-486f-b083-f34318ea5df9")}catch{}})();class r{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=r.instances.get(e.url);if(t)return t;const n=new r(e);return r.instances.set(e.url,n),n}handleMessage(e){try{const t=JSON.parse(e.data),n=this.parse?this.parse(t):t;this.messageHandlers.forEach(a=>{try{Promise.resolve(a(n)).catch(c)}catch(i){c(i)}})}catch(t){c(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}}const p="/_editor/api/codebase/events";function b(){return r.getInstance({url:p,parse:s=>f.parse(s)})}function g(){const s=o(!1),e=o(null),t=o(null),n=b(),a=u=>{e.value=u},i=()=>{s.value=n.isConnected()};let h;const d=setInterval(i,1e3);return v(()=>{h(),clearInterval(d)}),m(()=>{h=n.subscribe(a),i()}),{isConnected:l(s),lastEvent:l(e),error:l(t)}}export{r as W,g as u};
//# sourceMappingURL=useCodebaseEvents-DSPYpQr7.js.map
