mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-08 19:22:44 +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:
+3
-37
@@ -1,49 +1,15 @@
|
||||
import { event } from '@tauri-apps/api'
|
||||
import { listen, UnlistenFn, EventCallback } from '@tauri-apps/api/event'
|
||||
import { useCallback, useRef } from 'react'
|
||||
import { listen, EventCallback } from '@tauri-apps/api/event'
|
||||
import { useCallback } from 'react'
|
||||
|
||||
export const useListen = () => {
|
||||
const unlistenFnsRef = useRef<UnlistenFn[]>([])
|
||||
|
||||
const addListener = useCallback(
|
||||
async <T>(eventName: string, handler: EventCallback<T>) => {
|
||||
const unlisten = await listen(eventName, handler)
|
||||
unlistenFnsRef.current.push(unlisten)
|
||||
return unlisten
|
||||
return await listen(eventName, handler)
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
const removeAllListeners = useCallback(() => {
|
||||
const errors: Error[] = []
|
||||
|
||||
unlistenFnsRef.current.forEach((unlisten) => {
|
||||
try {
|
||||
unlisten()
|
||||
} catch (error) {
|
||||
errors.push(error instanceof Error ? error : new Error(String(error)))
|
||||
}
|
||||
})
|
||||
|
||||
if (errors.length > 0) {
|
||||
console.warn(
|
||||
`[useListen] 清理监听器时发生 ${errors.length} 个错误`,
|
||||
errors,
|
||||
)
|
||||
}
|
||||
|
||||
unlistenFnsRef.current.length = 0
|
||||
}, [])
|
||||
|
||||
const setupCloseListener = useCallback(async () => {
|
||||
await event.once('tauri://close-requested', async () => {
|
||||
removeAllListeners()
|
||||
})
|
||||
}, [removeAllListeners])
|
||||
|
||||
return {
|
||||
addListener,
|
||||
removeAllListeners,
|
||||
setupCloseListener,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,29 +104,14 @@ export const useProxySelection = (options: ProxySelectionOptions = {}) => {
|
||||
config.autoCloseConnection &&
|
||||
previousProxy
|
||||
) {
|
||||
setTimeout(() => cleanupConnections(previousProxy), 0)
|
||||
void cleanupConnections(previousProxy)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[ProxySelection] 代理切换失败: ${groupName} -> ${proxyName}`,
|
||||
error,
|
||||
)
|
||||
|
||||
try {
|
||||
await selectNodeForGroup(groupName, proxyName)
|
||||
onSuccess?.()
|
||||
syncTraySelection()
|
||||
persistSelection(groupName, proxyName, skipConfigSave)
|
||||
debugLog(
|
||||
`[ProxySelection] 代理切换回退成功: ${groupName} -> ${proxyName}`,
|
||||
)
|
||||
} catch (fallbackError) {
|
||||
console.error(
|
||||
`[ProxySelection] 代理切换回退也失败: ${groupName} -> ${proxyName}`,
|
||||
fallbackError,
|
||||
)
|
||||
onError?.(fallbackError)
|
||||
}
|
||||
onError?.(error)
|
||||
}
|
||||
},
|
||||
[config, onError, onSuccess, persistSelection, syncTraySelection],
|
||||
|
||||
Reference in New Issue
Block a user