mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 06:53:44 +08:00
chore: update
This commit is contained in:
parent
b9c8fa61b2
commit
1fb3b87697
@ -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
34
src/hooks/use-listen.ts
Normal 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,
|
||||||
|
};
|
||||||
|
};
|
@ -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":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user