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,