mirror of
https://github.com/fankes/komari-theme-purcarte.git
synced 2025-10-18 11:29:22 +08:00
fix: 修复私有状态处理逻辑
This commit is contained in:
@@ -5,8 +5,9 @@ import {
|
||||
useContext,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { wsService } from "../services/api";
|
||||
import { getWsService } from "../services/api";
|
||||
import type { NodeStats } from "../types/node";
|
||||
import { useNodeData } from "./NodeDataContext";
|
||||
|
||||
interface LiveData {
|
||||
online: string[];
|
||||
@@ -38,30 +39,37 @@ export const LiveDataProvider = ({
|
||||
enableWebSocket = true,
|
||||
}: LiveDataProviderProps) => {
|
||||
const [liveData, setLiveData] = useState<LiveData | null>(null);
|
||||
const { nodes, loading } = useNodeData();
|
||||
|
||||
useEffect(() => {
|
||||
if (!enableWebSocket) {
|
||||
// 只有在加载完成、站点非私有且启用了 WebSocket 时才连接
|
||||
if (!loading && nodes !== "private" && enableWebSocket) {
|
||||
const wsService = getWsService(); // 在需要时才获取实例
|
||||
console.log("连接------", loading, nodes, enableWebSocket);
|
||||
|
||||
const handleWebSocketData = (data: LiveData) => {
|
||||
if (data.online && data.data) {
|
||||
setLiveData({
|
||||
online: [...data.online],
|
||||
data: { ...data.data },
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const unsubscribe = wsService.subscribe(handleWebSocketData);
|
||||
wsService.connect();
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
wsService.disconnect(); // 确保在组件卸载或条件变化时断开连接
|
||||
};
|
||||
} else {
|
||||
// 如果条件不满足,确保断开任何现有连接
|
||||
const wsService = getWsService(); // 获取实例以调用 disconnect
|
||||
wsService.disconnect();
|
||||
setLiveData(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const handleWebSocketData = (data: LiveData) => {
|
||||
if (data.online && data.data) {
|
||||
setLiveData({
|
||||
online: [...data.online],
|
||||
data: { ...data.data },
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const unsubscribe = wsService.subscribe(handleWebSocketData);
|
||||
wsService.connect();
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
}, [enableWebSocket]);
|
||||
}, [loading, nodes, enableWebSocket]);
|
||||
|
||||
return (
|
||||
<LiveDataContext.Provider value={{ liveData }}>
|
||||
|
@@ -13,7 +13,7 @@ import type { NodeData, PublicInfo, HistoryRecord } from "../types/node";
|
||||
function useNodesInternal() {
|
||||
const [staticNodes, setStaticNodes] = useState<NodeData[] | "private">([]);
|
||||
const [publicSettings, setPublicSettings] = useState<PublicInfo | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchNodes = useCallback(async () => {
|
||||
|
@@ -231,5 +231,12 @@ export class WebSocketService {
|
||||
}
|
||||
}
|
||||
|
||||
// 创建 WebSocket 服务实例
|
||||
export const wsService = new WebSocketService();
|
||||
// 延迟 WebSocket 服务实例的创建
|
||||
let wsServiceInstance: WebSocketService | null = null;
|
||||
|
||||
export function getWsService(): WebSocketService {
|
||||
if (!wsServiceInstance) {
|
||||
wsServiceInstance = new WebSocketService();
|
||||
}
|
||||
return wsServiceInstance;
|
||||
}
|
||||
|
Reference in New Issue
Block a user