fix: when the window is hidden, close the websocket connection, reduce the risk of memory leaks

This commit is contained in:
huzibaca
2024-12-07 16:47:41 +08:00
parent 0e3b631118
commit b42d13f573
2 changed files with 50 additions and 45 deletions

View File

@@ -4,6 +4,7 @@ import { createSockette } from "../utils/websocket";
import { useClashInfo } from "./use-clash";
import dayjs from "dayjs";
import { create } from "zustand";
import { useVisibility } from "./use-visibility";
const MAX_LOG_NUM = 1000;
@@ -69,9 +70,10 @@ export const useLogData = (logLevel: LogLevel) => {
const { clashInfo } = useClashInfo();
const [enableLog] = useEnableLog();
const { logs, appendLog } = useLogStore();
const pageVisible = useVisibility();
useEffect(() => {
if (!enableLog || !clashInfo) return;
if (!enableLog || !clashInfo || !pageVisible) return;
const { server = "", secret = "" } = clashInfo;
const wsUrl = buildWSUrl(server, secret, logLevel);