perf(ui): hoist hot regexes and stabilize clash mutators

The clash log parser and the URI validators recompiled their patterns
per call; the patterns are module constants now. mutateClash and
invalidateClashConfig are stable callbacks so consumers can keep them
in dependency arrays; mutateClash revalidates through the module-level
query helper instead of the per-render refetch closure (no caller reads
its resolved value).
This commit is contained in:
Tunglies
2026-11-19 23:16:35 +08:00
parent 7074cc5c25
commit 2b812f5fa5
3 changed files with 35 additions and 23 deletions
+22 -12
View File
@@ -1,5 +1,6 @@
import { useLockFn } from 'ahooks'
import i18n from 'i18next'
import { useCallback } from 'react'
import { getVersion } from 'tauri-plugin-mihomo-api'
import {
@@ -86,23 +87,29 @@ export const useClashMode = (shouldFetch: boolean = true) => {
}
export const useClash = () => {
const { data: clash, refetch } = useRuntimeConfig()
const { data: clash } = useRuntimeConfig()
const { data: versionData, refetch: mutateVersion } = useQuery({
queryKey: ['getVersion'],
queryFn: getVersion,
})
const mutateClash = (updater?: MutateClashUpdater, revalidate?: boolean) => {
if (updater === undefined) {
return refetch()
}
void setCacheData<IConfigData>(['getRuntimeConfig'], updater)
if (revalidate !== false) {
return refetch()
}
return Promise.resolve()
}
// Stable identity: consumers pass mutateClash into their own callbacks.
// revalidateQuery hits the same cache key refetch would, without capturing
// the per-render refetch closure.
const mutateClash = useCallback(
(updater?: MutateClashUpdater, revalidate?: boolean) => {
if (updater === undefined) {
return revalidateQuery(['getRuntimeConfig'])
}
void setCacheData<IConfigData>(['getRuntimeConfig'], updater)
if (revalidate !== false) {
return revalidateQuery(['getRuntimeConfig'])
}
return Promise.resolve()
},
[],
)
const patchClash = useLockFn(async (patch: Partial<IConfigData>) => {
await mutate(() => patchClashConfig(patch), {
@@ -143,7 +150,10 @@ export const useClashInfo = () => {
})
})
const invalidateClashConfig = () => revalidateQuery(['getClashConfig'])
const invalidateClashConfig = useCallback(
() => revalidateQuery(['getClashConfig']),
[],
)
return {
clashInfo,
+6 -4
View File
@@ -177,13 +177,15 @@ export async function getProxyView(): Promise<ProxyViewV1> {
return view
}
// getClashLogs matches both patterns against every log line.
const CLASH_LOG_REGEX = /time="(.+?)"\s+level=(.+?)\s+msg="(.+?)"/
const CLASH_LOG_NEW_REGEX = /(.+?)\s+(.+?)\s+(.+)/
export async function getClashLogs() {
const regex = /time="(.+?)"\s+level=(.+?)\s+msg="(.+?)"/
const newRegex = /(.+?)\s+(.+?)\s+(.+)/
const logs = await commands.getClashLogs()
return logs.reduce<ILogItem[]>((acc, log) => {
const result = log.match(regex)
const result = log.match(CLASH_LOG_REGEX)
if (result) {
const [_, _time, type, payload] = result
const time = dayjs(_time).format('MM-DD HH:mm:ss')
@@ -191,7 +193,7 @@ export async function getClashLogs() {
return acc
}
const result2 = log.match(newRegex)
const result2 = log.match(CLASH_LOG_NEW_REGEX)
if (result2) {
const [_, time, type, payload] = result2
acc.push({ time, type, payload })
+7 -7
View File
@@ -241,17 +241,17 @@ export function parseUrlLike(
: result
}
// Both predicates run per parsed proxy URI.
const IPV4_REGEX = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
const IPV6_REGEX =
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::$|^::1$|^([0-9a-fA-F]{1,4}:)*::([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4}$/
export function isIPv4(address: string): boolean {
// Check if the address is IPv4
const ipv4Regex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
return ipv4Regex.test(address)
return IPV4_REGEX.test(address)
}
export function isIPv6(address: string): boolean {
// Check if the address is IPv6 - simplified regex to avoid backreference issues
const ipv6Regex =
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::$|^::1$|^([0-9a-fA-F]{1,4}:)*::([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4}$/
return ipv6Regex.test(address)
return IPV6_REGEX.test(address)
}
export function decodeBase64OrOriginal(str: string): string {