mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-06 03:03:44 +08:00
chore: update
This commit is contained in:
parent
d7f033bd46
commit
0a8be603c8
@ -1,34 +1,31 @@
|
|||||||
import { listen, UnlistenFn, EventCallback } from "@tauri-apps/api/event";
|
import { listen, UnlistenFn, EventCallback } from "@tauri-apps/api/event";
|
||||||
import { event } from "@tauri-apps/api";
|
import { event } from "@tauri-apps/api";
|
||||||
|
import { useRef } from "react";
|
||||||
|
|
||||||
export const useListen = () => {
|
export const useListen = () => {
|
||||||
let unlistenFns: UnlistenFn[] = [];
|
const unlistenFns = useRef<UnlistenFn[]>([]);
|
||||||
|
|
||||||
const addListener = async function <T>(
|
const addListener = async <T>(
|
||||||
eventName: string,
|
eventName: string,
|
||||||
handler: EventCallback<T>
|
handler: EventCallback<T>
|
||||||
) {
|
) => {
|
||||||
const unlisten = await listen(eventName, handler);
|
const unlisten = await listen(eventName, handler);
|
||||||
unlistenFns.push(unlisten);
|
unlistenFns.current.push(unlisten);
|
||||||
return unlisten;
|
return unlisten;
|
||||||
};
|
};
|
||||||
const removeAllListeners = async function () {
|
const removeAllListeners = () => {
|
||||||
for (const unlisten of unlistenFns) {
|
unlistenFns.current.forEach((unlisten) => unlisten());
|
||||||
Promise.resolve(unlisten()).catch(console.error);
|
unlistenFns.current = [];
|
||||||
}
|
|
||||||
unlistenFns = [];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const setupCloseListener = async function () {
|
const setupCloseListener = async function () {
|
||||||
await event.once("tauri://close-requested", async () => {
|
await event.once("tauri://close-requested", async () => {
|
||||||
console.log("Window close requested.");
|
removeAllListeners();
|
||||||
await removeAllListeners();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
addListener,
|
addListener,
|
||||||
removeAllListeners,
|
|
||||||
setupCloseListener,
|
setupCloseListener,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user