mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
feat: event emit when clash config update
This commit is contained in:
@@ -12,6 +12,7 @@ import useLogSetup from "./use-log-setup";
|
||||
import useTrafficGraph from "./use-traffic-graph";
|
||||
import parseTraffic from "../../utils/parse-traffic";
|
||||
|
||||
// setup the traffic
|
||||
const LayoutTraffic = () => {
|
||||
const portValue = useRecoilValue(atomClashPort);
|
||||
const [traffic, setTraffic] = useState({ up: 0, down: 0 });
|
||||
@@ -26,12 +27,14 @@ const LayoutTraffic = () => {
|
||||
useLogSetup();
|
||||
|
||||
useEffect(() => {
|
||||
let unlisten: () => void = null!;
|
||||
|
||||
// should reconnect the traffic ws
|
||||
listen("restart_clash", () => setRefresh({})).then((fn) => (unlisten = fn));
|
||||
const unlisten = listen("verge://refresh-clash-config", () =>
|
||||
setRefresh({})
|
||||
);
|
||||
|
||||
return () => unlisten?.();
|
||||
return () => {
|
||||
unlisten.then((fn) => fn());
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import dayjs from "dayjs";
|
||||
import { useEffect } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useSetRecoilState } from "recoil";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { ApiType } from "../../services/types";
|
||||
@@ -10,11 +10,11 @@ const MAX_LOG_NUM = 1000;
|
||||
|
||||
// setup the log websocket
|
||||
export default function useLogSetup() {
|
||||
const [refresh, setRefresh] = useState({});
|
||||
const setLogData = useSetRecoilState(atomLogData);
|
||||
|
||||
useEffect(() => {
|
||||
let ws: WebSocket = null!;
|
||||
let unlisten: () => void = null!;
|
||||
|
||||
const handler = (event: MessageEvent<any>) => {
|
||||
const data = JSON.parse(event.data) as ApiType.LogItem;
|
||||
@@ -25,25 +25,19 @@ export default function useLogSetup() {
|
||||
});
|
||||
};
|
||||
|
||||
(async () => {
|
||||
const { server = "", secret = "" } = await getInfomation();
|
||||
|
||||
getInfomation().then((info) => {
|
||||
const { server = "", secret = "" } = info;
|
||||
ws = new WebSocket(`ws://${server}/logs?token=${secret}`);
|
||||
ws.addEventListener("message", handler);
|
||||
});
|
||||
|
||||
// reconnect the websocket
|
||||
unlisten = await listen("restart_clash", async () => {
|
||||
const { server = "", secret = "" } = await getInfomation();
|
||||
|
||||
ws?.close();
|
||||
ws = new WebSocket(`ws://${server}/logs?token=${secret}`);
|
||||
ws.addEventListener("message", handler);
|
||||
});
|
||||
})();
|
||||
const unlisten = listen("verge://refresh-clash-config", () =>
|
||||
setRefresh({})
|
||||
);
|
||||
|
||||
return () => {
|
||||
ws?.close();
|
||||
unlisten?.();
|
||||
unlisten?.then((fn) => fn());
|
||||
};
|
||||
}, []);
|
||||
}, [refresh]);
|
||||
}
|
||||
|
||||
@@ -30,12 +30,10 @@ const Layout = () => {
|
||||
if (e.key === "Escape") appWindow.hide();
|
||||
});
|
||||
|
||||
listen("restart_clash", async () => {
|
||||
listen("verge://refresh-clash-config", async () => {
|
||||
// the clash info may be updated
|
||||
await getAxios(true);
|
||||
// make sure that the clash is ok
|
||||
setTimeout(() => mutate("getProxies"), 1000);
|
||||
setTimeout(() => mutate("getProxies"), 2000);
|
||||
mutate("getProxies");
|
||||
mutate("getClashConfig");
|
||||
});
|
||||
}, []);
|
||||
|
||||
Reference in New Issue
Block a user