Files
clash-verge-rev/src/hooks/use-clash.ts
T
Tunglies 3048a2ae08 feat: Implement configuration caching mechanism and force-refresh feature**
✨ **New Features**:

* Added API and frontend support for forcibly refreshing Clash configuration cache
* Implemented a configuration cache TTL mechanism (60 seconds) to reduce redundant requests
* Introduced `ProxyRequestCache` system to manage backend data caching
* Automatically refresh frontend state after core operations to enhance user experience

🚀 **Performance Optimizations**:

* Increased Clash configuration refresh interval from 5 seconds to 60 seconds
* Force refresh cache after configuration updates to resolve data inconsistency
* Automatically trigger state refresh after core switch, start, stop, and restart actions

🔧 **Technical Improvements**:

* Removed unused dependencies: `ab_glyph`, `owned_ttf_parser`, `ttf-parser`
* Simplified WebSocket dependency management, unified `tungstenite` version
* Refactored configuration save validation process, improved merge file handling
* Improved error handling and overall user experience
2025-07-24 01:59:25 +08:00

137 lines
3.1 KiB
TypeScript

import useSWR, { mutate } from "swr";
import { useLockFn } from "ahooks";
import { getVersion } from "@/services/cmds";
import {
getClashInfo,
patchClashConfig,
getRuntimeConfig,
forceRefreshClashConfig,
} from "@/services/cmds";
export const useClash = () => {
const { data: clash, mutate: mutateClash } = useSWR(
"getRuntimeConfig",
getRuntimeConfig,
);
const { data: versionData, mutate: mutateVersion } = useSWR(
"getVersion",
getVersion,
);
const patchClash = useLockFn(async (patch: Partial<IConfigData>) => {
await patchClashConfig(patch);
mutateClash();
});
const version = versionData?.premium
? `${versionData.version} Premium`
: versionData?.meta
? `${versionData.version} Mihomo`
: versionData?.version || "-";
return {
clash,
version,
mutateClash,
mutateVersion,
patchClash,
};
};
export const useClashInfo = () => {
const { data: clashInfo, mutate: mutateInfo } = useSWR(
"getClashInfo",
getClashInfo,
);
const patchInfo = async (
patch: Partial<
Pick<
IConfigData,
| "port"
| "socks-port"
| "mixed-port"
| "redir-port"
| "tproxy-port"
| "external-controller"
| "secret"
>
>,
) => {
const hasInfo =
patch["redir-port"] != null ||
patch["tproxy-port"] != null ||
patch["mixed-port"] != null ||
patch["socks-port"] != null ||
patch["port"] != null ||
patch["external-controller"] != null ||
patch.secret != null;
if (!hasInfo) return;
if (patch["redir-port"]) {
const port = patch["redir-port"];
if (port < 1111) {
throw new Error("The port should not < 1111");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
if (patch["tproxy-port"]) {
const port = patch["tproxy-port"];
if (port < 1111) {
throw new Error("The port should not < 1111");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
if (patch["mixed-port"]) {
const port = patch["mixed-port"];
if (port < 1111) {
throw new Error("The port should not < 1111");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
if (patch["socks-port"]) {
const port = patch["socks-port"];
if (port < 1111) {
throw new Error("The port should not < 1111");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
if (patch["port"]) {
const port = patch["port"];
if (port < 1111) {
throw new Error("The port should not < 1111");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
await patchClashConfig(patch);
mutateInfo();
// 配置修改后强制刷新缓存
await forceRefreshClashConfig();
mutate("getClashConfig");
// IPC调用不需要刷新axios实例
};
return {
clashInfo,
mutateInfo,
patchInfo,
};
};