mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-06 21:04:20 +00:00
fix(inbounds): keep last speed across page navigation instead of blanking
Speed is delta-derived, so it can't be recomputed until the first poll after mount. The websocket subscription and speed state are page-scoped (useWebSocket lives in InboundsPage), so leaving to another page and returning blanked the Speed column for up to one 5s poll. Cache the last speed map across mounts (module scope, 15s recency guard) and seed the state from it, so returning shows the last throughput immediately and the next poll refreshes it. Applies to both local and node-hosted inbound speed.
This commit is contained in:
@@ -32,6 +32,14 @@ type DBInboundInstance = InstanceType<typeof DBInbound>;
|
||||
// deltas accumulated over this window, so dividing by it yields bytes/sec.
|
||||
const TRAFFIC_POLL_INTERVAL_S = 5;
|
||||
|
||||
// Speed is delta-derived, so it can't be recomputed until the first poll after
|
||||
// mount; navigating away and back would otherwise blank the column for up to one
|
||||
// poll. Cache the last speed map across mounts (module scope) and reseed from it
|
||||
// while recent, so returning to the page shows the last throughput immediately
|
||||
// and the next poll refreshes it.
|
||||
const SPEED_CACHE_TTL_MS = 15000;
|
||||
let inboundSpeedCache: { at: number; data: Record<number, InboundSpeedEntry> } = { at: 0, data: {} };
|
||||
|
||||
interface TrafficDelta {
|
||||
Tag: string;
|
||||
Up: number;
|
||||
@@ -191,7 +199,12 @@ export function useInbounds() {
|
||||
const [clientCount, setClientCount] = useState<Record<number, ClientRollup>>({});
|
||||
const [statsVersion, setStatsVersion] = useState(0);
|
||||
|
||||
const [inboundSpeed, setInboundSpeed] = useState<Record<number, InboundSpeedEntry>>({});
|
||||
const [inboundSpeed, setInboundSpeed] = useState<Record<number, InboundSpeedEntry>>(() =>
|
||||
Date.now() - inboundSpeedCache.at < SPEED_CACHE_TTL_MS ? inboundSpeedCache.data : {},
|
||||
);
|
||||
useEffect(() => {
|
||||
inboundSpeedCache = { at: Date.now(), data: inboundSpeed };
|
||||
}, [inboundSpeed]);
|
||||
|
||||
const [onlineClients, setOnlineClients] = useState<string[]>([]);
|
||||
const onlineClientsRef = useRef<string[]>([]);
|
||||
|
||||
Reference in New Issue
Block a user