2 lines
1.8 KiB
JavaScript
2 lines
1.8 KiB
JavaScript
import{c as s}from"./index-CuOHsLf7.js";import{g as a,u as n}from"./unified-ws-CBnrIqHW.js";class c{connectionCallbacks=new Set;initialized=!1;isConnected=!1;logCache=[];logCallbacks=new Set;subscriptionActive=!1;getMaxCacheSize(){return a("logCacheSize")}initialize(){this.initialized||(n.addEventListener(t=>{if(t.domain==="logs"){if(t.event==="snapshot"){const e=Array.isArray(t.data.entries)?t.data.entries:[];this.logCache=e.slice(-this.getMaxCacheSize()),this.notifyLogChange();return}t.event==="entry"&&t.data.entry&&this.appendLog(t.data.entry)}}),n.onConnectionChange(t=>{this.isConnected=t,this.notifyConnection(t)}),this.initialized=!0)}appendLog(t){if(this.logCache.some(o=>o.id===t.id))return;this.logCache.push(t);const i=this.getMaxCacheSize();this.logCache.length>i&&(this.logCache=this.logCache.slice(-i)),this.notifyLogChange()}notifyLogChange(){this.logCallbacks.forEach(t=>{try{t()}catch(e){console.error("日志回调执行失败:",e)}})}notifyConnection(t){this.connectionCallbacks.forEach(e=>{try{e(t)}catch(i){console.error("连接状态回调执行失败:",i)}})}async connect(){if(!(window.location.pathname==="/auth"||!await s())&&(this.initialize(),!this.subscriptionActive))try{await n.subscribe("logs","main",{replay:100}),this.subscriptionActive=!0}catch(e){console.error("订阅日志流失败:",e)}}disconnect(){this.subscriptionActive=!1,n.unsubscribe("logs","main"),this.isConnected=!1,this.notifyConnection(!1)}onLog(t){return this.logCallbacks.add(t),()=>this.logCallbacks.delete(t)}onConnectionChange(t){return this.connectionCallbacks.add(t),t(this.isConnected),()=>this.connectionCallbacks.delete(t)}getAllLogs(){return[...this.logCache]}clearLogs(){this.logCache=[],this.notifyLogChange()}getConnectionStatus(){return this.isConnected}}const r=new c;typeof window<"u"&&setTimeout(()=>{r.connect()},100);export{r as l};
|