合并页面恢复钱包刷新
将可见性与焦点事件合并到同一零延迟刷新任务,避免前台切换产生连续请求。 卸载时清理待执行定时器,并补充双事件回归测试。
This commit is contained in:
@@ -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'));
|
||||
});
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user