테블릿 로딩 이슈 수정

This commit is contained in:
Macbook
2026-05-31 23:44:31 +09:00
parent a34b49c23a
commit 332019866a
7 changed files with 78 additions and 15 deletions
+18 -1
View File
@@ -64,6 +64,12 @@ function listenerCount(): number {
return listeners.size;
}
function mayOpenSocket(): boolean {
if (listenerCount() === 0) return false;
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return false;
return true;
}
function cancelPendingDisconnect(): void {
if (disconnectTimer) {
clearTimeout(disconnectTimer);
@@ -134,7 +140,7 @@ function dispatch(text: string): void {
}
function scheduleReconnect(): void {
if (listenerCount() === 0) return;
if (!mayOpenSocket()) return;
if (reconnectTimer) return;
const delay = Math.min(RECONNECT_BASE * 2 ** retryCount, RECONNECT_MAX);
retryCount += 1;
@@ -236,6 +242,17 @@ export function subscribeUpbitWsConnection(listener: ConnectionListener): () =>
return () => connectionListeners.delete(listener);
}
if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
cancelPendingConnect();
if (listenerCount() === 0) teardownSocket();
return;
}
if (mayOpenSocket()) scheduleConnect();
});
}
/**
* 업비트 WS 채널 구독 (참조 카운트). codes 가 비어 있으면 구독하지 않음.
*/