chore: update

This commit is contained in:
huzibaca 2024-09-25 21:07:01 +08:00
parent b9c8fa61b2
commit 1fb3b87697
3 changed files with 44 additions and 5 deletions

View File

@ -20,7 +20,7 @@ import delayManager from "@/services/delay";
import { cmdTestDelay, downloadIconCache } from "@/services/cmds"; import { cmdTestDelay, downloadIconCache } from "@/services/cmds";
import { listen, UnlistenFn } from "@tauri-apps/api/event"; import { listen, UnlistenFn } from "@tauri-apps/api/event";
import { convertFileSrc } from "@tauri-apps/api/core"; import { convertFileSrc } from "@tauri-apps/api/core";
import { useListen } from "@/hooks/use-listen";
interface Props { interface Props {
id: string; id: string;
itemData: IVergeTestItem; itemData: IVergeTestItem;
@ -47,6 +47,7 @@ export const TestItem = (props: Props) => {
const [delay, setDelay] = useState(-1); const [delay, setDelay] = useState(-1);
const { uid, name, icon, url } = itemData; const { uid, name, icon, url } = itemData;
const [iconCachePath, setIconCachePath] = useState(""); const [iconCachePath, setIconCachePath] = useState("");
const { addListener } = useListen();
useEffect(() => { useEffect(() => {
initIconCachePath(); initIconCachePath();
@ -91,7 +92,7 @@ export const TestItem = (props: Props) => {
const listenTsetEvent = async () => { const listenTsetEvent = async () => {
eventListener(); eventListener();
eventListener = await listen("verge://test-all", () => { eventListener = await addListener("verge://test-all", () => {
onDelay(); onDelay();
}); });
}; };

34
src/hooks/use-listen.ts Normal file
View File

@ -0,0 +1,34 @@
import { listen, UnlistenFn, EventCallback } from "@tauri-apps/api/event";
import { event } from "@tauri-apps/api";
export const useListen = () => {
let unlistenFns: UnlistenFn[] = [];
const addListener = async function <T>(
eventName: string,
handler: EventCallback<T>
) {
const unlisten = await listen(eventName, handler);
unlistenFns.push(unlisten);
return unlisten;
};
const removeAllListeners = async function () {
for (const unlisten of unlistenFns) {
Promise.resolve(unlisten()).catch(console.error);
}
unlistenFns = [];
};
const setupCloseListener = async function () {
await event.once("tauri://close-requested", async () => {
console.log("Window close requested.");
await removeAllListeners();
});
};
return {
addListener,
removeAllListeners,
setupCloseListener,
};
};

View File

@ -27,6 +27,7 @@ import "dayjs/locale/zh-cn";
import { getPortableFlag } from "@/services/cmds"; import { getPortableFlag } from "@/services/cmds";
import React from "react"; import React from "react";
import { TransitionGroup, CSSTransition } from "react-transition-group"; import { TransitionGroup, CSSTransition } from "react-transition-group";
import { useListen } from "@/hooks/use-listen";
const appWindow = getCurrentWebviewWindow(); const appWindow = getCurrentWebviewWindow();
export let portableFlag = false; export let portableFlag = false;
@ -46,10 +47,13 @@ const Layout = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
const routersEles = useRoutes(routers); const routersEles = useRoutes(routers);
const { addListener, setupCloseListener } = useListen();
if (!routersEles) return null; if (!routersEles) return null;
setupCloseListener();
useEffect(() => { useEffect(() => {
listen("verge://refresh-clash-config", async () => { addListener("verge://refresh-clash-config", async () => {
// the clash info may be updated // the clash info may be updated
await getAxios(true); await getAxios(true);
mutate("getProxies"); mutate("getProxies");
@ -59,10 +63,10 @@ const Layout = () => {
}); });
// update the verge config // update the verge config
listen("verge://refresh-verge-config", () => mutate("getVergeConfig")); addListener("verge://refresh-verge-config", () => mutate("getVergeConfig"));
// 设置提示监听 // 设置提示监听
listen("verge://notice-message", ({ payload }) => { addListener("verge://notice-message", ({ payload }) => {
const [status, msg] = payload as [string, string]; const [status, msg] = payload as [string, string];
switch (status) { switch (status) {
case "import_sub_url::ok": case "import_sub_url::ok":