refactor: migrate query cache to swr

This commit is contained in:
Tunglies
2026-06-28 19:39:15 +08:00
parent 294955cb8c
commit e71e701fd0
18 changed files with 213 additions and 58 deletions
+1 -1
View File
@@ -1,4 +1,3 @@
import { useQuery } from '@tanstack/react-query'
import { useLockFn } from 'ahooks'
import { getVersion } from 'tauri-plugin-mihomo-api'
@@ -8,6 +7,7 @@ import {
getRuntimeConfig,
patchClashConfig,
} from '@/services/cmds'
import { useQuery } from '@/services/query-client'
import { queryClient } from '@/services/query-client'
type MutateClashUpdater =
+1 -1
View File
@@ -1,8 +1,8 @@
import { useQuery } from '@tanstack/react-query'
import { convertFileSrc } from '@tauri-apps/api/core'
import { useMemo } from 'react'
import { downloadIconCache } from '@/services/cmds'
import { useQuery } from '@/services/query-client'
export interface UseIconCacheOptions {
icon?: string | null
+1 -2
View File
@@ -1,9 +1,9 @@
import { useQueryClient } from '@tanstack/react-query'
import dayjs from 'dayjs'
import { useEffect, useRef } from 'react'
import { MihomoWebSocket, type LogLevel } from 'tauri-plugin-mihomo-api'
import { getClashLogs } from '@/services/cmds'
import { queryClient } from '@/services/query-client'
import { useClashLog } from './use-clash-log'
import { useMihomoWsSubscription } from './use-mihomo-ws-subscription'
@@ -48,7 +48,6 @@ const appendLogs = (
}
export const useLogData = () => {
const queryClient = useQueryClient()
const [clashLog] = useClashLog()
const enableLog = clashLog.enable
const logLevel = clashLog.logLevel.toUpperCase() as LogLevel
+4 -5
View File
@@ -1,8 +1,9 @@
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useLocalStorage } from 'foxact/use-local-storage'
import { type MutableRefObject, useCallback, useEffect, useRef } from 'react'
import { type Message, type MihomoWebSocket } from 'tauri-plugin-mihomo-api'
import { queryClient, useQuery } from '@/services/query-client'
export const RECONNECT_DELAY_MS = 1000
interface SharedSubscriptionOwner {
@@ -195,8 +196,6 @@ export const useMihomoWsSubscription = <T>(
const responseCacheKey =
subscriptionCacheKey ?? lastSubscriptionCacheKeyRef.current
const queryClient = useQueryClient()
const wsRef = useRef<MihomoWebSocket | null>(null)
const resolveNextData = useCallback(
@@ -211,7 +210,7 @@ export const useMihomoWsSubscription = <T>(
}
return data ?? fallbackData
},
[queryClient, fallbackData],
[fallbackData],
)
const response = useQuery<T>({
@@ -355,7 +354,7 @@ export const useMihomoWsSubscription = <T>(
queryClient.removeQueries({ queryKey: [subscriptionCacheKey] })
}
setDate(Date.now())
}, [queryClient, subscriptionCacheKey, setDate])
}, [subscriptionCacheKey, setDate])
return { response, refresh, subscriptionCacheKey: responseCacheKey, wsRef }
}
+1 -2
View File
@@ -1,6 +1,5 @@
import { useQuery } from '@tanstack/react-query'
import { getNetworkInterfacesInfo } from '@/services/cmds'
import { useQuery } from '@/services/query-client'
export const useNetworkInterfaces = () => {
const {
+1 -1
View File
@@ -1,4 +1,3 @@
import { useQuery } from '@tanstack/react-query'
import { selectNodeForGroup } from 'tauri-plugin-mihomo-api'
import {
@@ -7,6 +6,7 @@ import {
patchProfile,
patchProfilesConfig,
} from '@/services/cmds'
import { useQuery } from '@/services/query-client'
import { queryClient } from '@/services/query-client'
import { debugLog } from '@/utils/debug'
+1 -1
View File
@@ -1,10 +1,10 @@
import { useQuery } from '@tanstack/react-query'
import { useRef } from 'react'
import { closeAllConnections } from 'tauri-plugin-mihomo-api'
import { useVerge } from '@/hooks/use-verge'
import { useClashConfigData, useSystemData } from '@/providers/app-data-context'
import { getAutotemProxy } from '@/services/cmds'
import { useQuery } from '@/services/query-client'
import { queryClient } from '@/services/query-client'
// 系统代理状态检测统一逻辑
+1 -1
View File
@@ -1,8 +1,8 @@
import { useQuery } from '@tanstack/react-query'
import { useEffect, useRef, useState } from 'react'
import { getRunningMode, isAdmin, isServiceAvailable } from '@/services/cmds'
import { showNotice } from '@/services/notice-service'
import { useQuery } from '@/services/query-client'
import { useVerge } from './use-verge'
+1 -2
View File
@@ -1,5 +1,4 @@
import { useQuery } from '@tanstack/react-query'
import { useQuery } from '@/services/query-client'
import { queryClient } from '@/services/query-client'
import { checkUpdateSafe } from '@/services/update'
+4 -5
View File
@@ -1,11 +1,10 @@
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useCallback } from 'react'
import { getVergeConfig, patchVergeConfig } from '@/services/cmds'
import { getPreloadConfig, setPreloadConfig } from '@/services/preload'
import { queryClient, useQuery } from '@/services/query-client'
export const useVerge = () => {
const qc = useQueryClient()
const initialVergeConfig = getPreloadConfig()
const { data: verge, refetch } = useQuery({
@@ -31,11 +30,11 @@ export const useVerge = () => {
return
}
if (typeof updaterOrData === 'function') {
const prev = qc.getQueryData<IVergeConfig>(['getVergeConfig'])
const prev = queryClient.getQueryData<IVergeConfig>(['getVergeConfig'])
const next = updaterOrData(prev)
qc.setQueryData(['getVergeConfig'], next)
queryClient.setQueryData(['getVergeConfig'], next)
} else {
qc.setQueryData(['getVergeConfig'], updaterOrData)
queryClient.setQueryData(['getVergeConfig'], updaterOrData)
}
}