From 999ab8e5e8371fe2a5e0cc52a3a853c21177c1bf Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 21 May 2026 22:36:04 +0800 Subject: [PATCH] fix profile auto update refresh --- src/components/profile/profile-item.tsx | 81 +++++++++++++------------ 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/src/components/profile/profile-item.tsx b/src/components/profile/profile-item.tsx index 4438831c1..4586ed6f0 100644 --- a/src/components/profile/profile-item.tsx +++ b/src/components/profile/profile-item.tsx @@ -16,6 +16,7 @@ import { MenuItem, Typography, } from '@mui/material' +import { listen } from '@tauri-apps/api/event' import { open } from '@tauri-apps/plugin-shell' import { useLockFn } from 'ahooks' import dayjs from 'dayjs' @@ -201,11 +202,10 @@ export const ProfileItem = (props: Props) => { // 订阅定时器更新事件 useEffect(() => { - // 处理定时器更新事件 - 这个事件专门用于通知定时器变更 - const handleTimerUpdate = (event: Event) => { - const source = event as CustomEvent & { payload?: string } - const updatedUid = source.detail ?? source.payload + let disposed = false + let unlistenTimerUpdate: (() => void) | undefined + listen('verge://timer-updated', ({ payload: updatedUid }) => { // 只有当更新的是当前配置时才刷新显示 if (updatedUid === itemData.uid && showNextUpdateRef.current) { debugLog(`收到定时器更新事件: uid=${updatedUid}`) @@ -216,17 +216,22 @@ export const ProfileItem = (props: Props) => { fetchNextUpdateTime(true) }, 1000) } - } - - // 只注册定时器更新事件监听 - window.addEventListener('verge://timer-updated', handleTimerUpdate) + }) + .then((unlisten) => { + if (disposed) { + unlisten() + return + } + unlistenTimerUpdate = unlisten + }) + .catch(console.error) return () => { + disposed = true if (refreshTimeoutRef.current !== undefined) { clearTimeout(refreshTimeoutRef.current) } - // 清理事件监听 - window.removeEventListener('verge://timer-updated', handleTimerUpdate) + unlistenTimerUpdate?.() } }, [fetchNextUpdateTime, itemData.uid]) @@ -588,45 +593,43 @@ export const ProfileItem = (props: Props) => { // 监听自动更新事件 useEffect(() => { - const handleUpdateStarted = (event: Event) => { - const customEvent = event as CustomEvent<{ uid?: string }> - if (customEvent.detail?.uid === itemData.uid) { - setLoadingCache((cache) => ({ ...cache, [itemData.uid]: true })) - } - } + let disposed = false + let unlisteners: Array<() => void> = [] + + Promise.all([ + listen<{ uid?: string }>('profile-update-started', ({ payload }) => { + if (payload.uid === itemData.uid) { + setLoadingCache((cache) => ({ ...cache, [itemData.uid]: true })) + } + }), + listen<{ uid?: string }>('profile-update-completed', ({ payload }) => { + if (payload.uid !== itemData.uid) { + return + } - const handleUpdateCompleted = (event: Event) => { - const customEvent = event as CustomEvent<{ uid?: string }> - if (customEvent.detail?.uid === itemData.uid) { setLoadingCache((cache) => ({ ...cache, [itemData.uid]: false })) // 刷新 profile 数据以获取最新的 updated 时间戳 void mutateProfiles() // 更新完成后刷新显示 - if (showNextUpdate) { + if (showNextUpdateRef.current) { fetchNextUpdateTime() } - } - } - - // 注册事件监听 - window.addEventListener('profile-update-started', handleUpdateStarted) - window.addEventListener('profile-update-completed', handleUpdateCompleted) + }), + ]) + .then((registeredUnlisteners) => { + if (disposed) { + registeredUnlisteners.forEach((unlisten) => unlisten()) + return + } + unlisteners = registeredUnlisteners + }) + .catch(console.error) return () => { - // 清理事件监听 - window.removeEventListener('profile-update-started', handleUpdateStarted) - window.removeEventListener( - 'profile-update-completed', - handleUpdateCompleted, - ) + disposed = true + unlisteners.forEach((unlisten) => unlisten()) } - }, [ - fetchNextUpdateTime, - itemData.uid, - mutateProfiles, - setLoadingCache, - showNextUpdate, - ]) + }, [fetchNextUpdateTime, itemData.uid, mutateProfiles, setLoadingCache]) const handleSaveProfileDocument = useLockFn(async () => { const currentValue = profileDocument.value