mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-17 14:17:37 +08:00
- 后端为失败数据库连接增加冷却窗口,避免短时间内重复真实建连 - 补充失败冷却回归测试,覆盖重复失败、冷却后重试和成功后清理场景 - 前端在后台态暂停查询页、侧边栏和表概览的自动元数据拉取 - 保持手动刷新、手动展开等显式操作行为不变
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
type AutoFetchVisibilitySource = Partial<Pick<Document, 'hidden' | 'visibilityState'>> | undefined;
|
|
|
|
export const isAutoFetchVisible = (source?: AutoFetchVisibilitySource): boolean => {
|
|
if (!source) {
|
|
return true;
|
|
}
|
|
|
|
if (source.hidden === true) {
|
|
return false;
|
|
}
|
|
|
|
if (source.visibilityState && source.visibilityState !== 'visible') {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
const getDocumentAutoFetchVisibility = (): boolean => {
|
|
if (typeof document === 'undefined') {
|
|
return true;
|
|
}
|
|
|
|
return isAutoFetchVisible(document);
|
|
};
|
|
|
|
export const useAutoFetchVisibility = (): boolean => {
|
|
const [isVisible, setIsVisible] = useState<boolean>(() => getDocumentAutoFetchVisibility());
|
|
|
|
useEffect(() => {
|
|
if (typeof document === 'undefined') {
|
|
return undefined;
|
|
}
|
|
|
|
const syncVisibility = () => {
|
|
setIsVisible(getDocumentAutoFetchVisibility());
|
|
};
|
|
|
|
syncVisibility();
|
|
document.addEventListener('visibilitychange', syncVisibility);
|
|
window.addEventListener('focus', syncVisibility);
|
|
window.addEventListener('pageshow', syncVisibility);
|
|
|
|
return () => {
|
|
document.removeEventListener('visibilitychange', syncVisibility);
|
|
window.removeEventListener('focus', syncVisibility);
|
|
window.removeEventListener('pageshow', syncVisibility);
|
|
};
|
|
}, []);
|
|
|
|
return isVisible;
|
|
};
|