From a6c5c901a137ab6b52e4b9eaee2ff112823f7939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E5=AE=87?= Date: Wed, 5 Aug 2026 17:41:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E9=A1=B5=E9=9D=A2=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E9=92=B1=E5=8C=85=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将可见性与焦点事件合并到同一零延迟刷新任务,避免前台切换产生连续请求。 卸载时清理待执行定时器,并补充双事件回归测试。 --- .../PlatformEntryActiveFlowShell.test.tsx | 3 ++- src/stores/usePlatformWalletStore.ts | 20 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/components/platform-entry/PlatformEntryActiveFlowShell.test.tsx b/src/components/platform-entry/PlatformEntryActiveFlowShell.test.tsx index 399141c47..cb836cc9d 100644 --- a/src/components/platform-entry/PlatformEntryActiveFlowShell.test.tsx +++ b/src/components/platform-entry/PlatformEntryActiveFlowShell.test.tsx @@ -267,7 +267,7 @@ describe('PlatformEntryActiveFlowShell', () => { await screen.findByLabelText('泥点 67'); }); - it('refreshes the shared wallet when the authenticated page regains focus', async () => { + it('coalesces visibility and focus refreshes when the page returns to the foreground', async () => { authUiMock.value.user = { id: 'user-1', publicUserCode: '100001', @@ -300,6 +300,7 @@ describe('PlatformEntryActiveFlowShell', () => { await screen.findByLabelText('泥点 10'); await act(async () => { + document.dispatchEvent(new Event('visibilitychange')); window.dispatchEvent(new Event('focus')); }); diff --git a/src/stores/usePlatformWalletStore.ts b/src/stores/usePlatformWalletStore.ts index 89cb7017e..ff58a7b16 100644 --- a/src/stores/usePlatformWalletStore.ts +++ b/src/stores/usePlatformWalletStore.ts @@ -28,20 +28,30 @@ export function usePlatformWalletLifecycle( } void onWalletBalanceMayHaveChanged(); - const refreshWallet = () => { - void onWalletBalanceMayHaveChanged(); + let foregroundRefreshTimer: number | null = null; + const scheduleWalletRefresh = () => { + if (foregroundRefreshTimer !== null) { + return; + } + foregroundRefreshTimer = window.setTimeout(() => { + foregroundRefreshTimer = null; + void onWalletBalanceMayHaveChanged(); + }, 0); }; const refreshVisibleWallet = () => { if (document.visibilityState === 'visible') { - refreshWallet(); + scheduleWalletRefresh(); } }; - window.addEventListener('focus', refreshWallet); + window.addEventListener('focus', scheduleWalletRefresh); document.addEventListener('visibilitychange', refreshVisibleWallet); return () => { - window.removeEventListener('focus', refreshWallet); + window.removeEventListener('focus', scheduleWalletRefresh); document.removeEventListener('visibilitychange', refreshVisibleWallet); + if (foregroundRefreshTimer !== null) { + window.clearTimeout(foregroundRefreshTimer); + } }; }, [ canAccessProtectedData,