mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-24 14:32:13 +08:00
refactor: frontend (#7097)
* refactor: remove unused window show/hide event listeners * refactor: deduplicate profile-changed event handling * refactor: remove unused setupCloseListener from use-listen * refactor: remove unused removeAllListeners from use-listen * fix(backend): dedupe profile changed notification on import * fix: avoid duplicate timer update completed event * refactor(frontend): simplify proxy switch fallback path
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import { listen } from '@tauri-apps/api/event'
|
||||
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
|
||||
import { useEffect } from 'react'
|
||||
|
||||
import { useListen } from '@/hooks/use-listen'
|
||||
@@ -73,20 +71,6 @@ export const useLayoutEvents = (
|
||||
),
|
||||
)
|
||||
|
||||
const appWindow = getCurrentWebviewWindow()
|
||||
register(
|
||||
(async () => {
|
||||
const [hideUnlisten, showUnlisten] = await Promise.all([
|
||||
listen('verge://hide-window', () => appWindow.hide()),
|
||||
listen('verge://show-window', () => appWindow.show()),
|
||||
])
|
||||
return () => {
|
||||
hideUnlisten()
|
||||
showUnlisten()
|
||||
}
|
||||
})(),
|
||||
)
|
||||
|
||||
return () => {
|
||||
disposed = true
|
||||
const errors: Error[] = []
|
||||
|
||||
+1
-54
@@ -22,7 +22,7 @@ import {
|
||||
} from '@mui/icons-material'
|
||||
import { Box, Button, Divider, Grid, IconButton, Stack } from '@mui/material'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { listen, TauriEvent } from '@tauri-apps/api/event'
|
||||
import { TauriEvent } from '@tauri-apps/api/event'
|
||||
import { readText } from '@tauri-apps/plugin-clipboard-manager'
|
||||
import { readTextFile } from '@tauri-apps/plugin-fs'
|
||||
import { useLockFn } from 'ahooks'
|
||||
@@ -742,59 +742,6 @@ const ProfilePage = () => {
|
||||
? 'rgba(0, 0, 0, 0.06)'
|
||||
: 'rgba(255, 255, 255, 0.06)'
|
||||
|
||||
// 监听后端配置变更
|
||||
useEffect(() => {
|
||||
let unlistenPromise: Promise<() => void> | undefined
|
||||
let lastProfileId: string | null = null
|
||||
let lastUpdateTime = 0
|
||||
const debounceDelay = 200
|
||||
|
||||
let refreshTimer: number | null = null
|
||||
|
||||
const setupListener = async () => {
|
||||
unlistenPromise = listen<string>('profile-changed', (event) => {
|
||||
const newProfileId = event.payload
|
||||
const now = Date.now()
|
||||
|
||||
debugLog(`[Profile] 收到配置变更事件: ${newProfileId}`)
|
||||
|
||||
if (
|
||||
lastProfileId === newProfileId &&
|
||||
now - lastUpdateTime < debounceDelay
|
||||
) {
|
||||
debugLog(`[Profile] 重复事件被防抖,跳过`)
|
||||
return
|
||||
}
|
||||
|
||||
lastProfileId = newProfileId
|
||||
lastUpdateTime = now
|
||||
|
||||
debugLog(`[Profile] 执行配置数据刷新`)
|
||||
|
||||
if (refreshTimer !== null) {
|
||||
window.clearTimeout(refreshTimer)
|
||||
}
|
||||
|
||||
// 使用异步调度避免阻塞事件处理
|
||||
refreshTimer = window.setTimeout(() => {
|
||||
mutateProfiles().catch((error) => {
|
||||
console.error('[Profile] 配置数据刷新失败:', error)
|
||||
})
|
||||
refreshTimer = null
|
||||
}, 0)
|
||||
})
|
||||
}
|
||||
|
||||
setupListener()
|
||||
|
||||
return () => {
|
||||
if (refreshTimer !== null) {
|
||||
window.clearTimeout(refreshTimer)
|
||||
}
|
||||
unlistenPromise?.then((unlisten) => unlisten()).catch(console.error)
|
||||
}
|
||||
}, [mutateProfiles])
|
||||
|
||||
// 组件卸载时清理中断控制器
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
|
||||
Reference in New Issue
Block a user