B: Fix potential memory leaks

This commit is contained in:
Ivan 2025-11-06 15:26:10 +03:00
parent ef9d8f31ec
commit b6c94cfe73
2 changed files with 24 additions and 8 deletions

View File

@ -1,5 +1,7 @@
'use client'; 'use client';
import { useEffect } from 'react';
import { urls, useConceptNavigation } from '@/app'; import { urls, useConceptNavigation } from '@/app';
import { useAuthSuspense } from '@/features/auth'; import { useAuthSuspense } from '@/features/auth';
@ -9,12 +11,14 @@ export function HomePage() {
const router = useConceptNavigation(); const router = useConceptNavigation();
const { isAnonymous } = useAuthSuspense(); const { isAnonymous } = useAuthSuspense();
if (isAnonymous) { useEffect(() => {
// Note: Timeout is needed to let router initialize // Note: Timeout is needed to let router initialize
setTimeout(() => router.replace({ path: urls.login }), PARAMETER.minimalTimeout); const timeoutId = setTimeout(() => {
} else { router.replace({ path: isAnonymous ? urls.login : urls.library });
setTimeout(() => router.replace({ path: urls.library }), PARAMETER.minimalTimeout); }, PARAMETER.minimalTimeout);
}
return () => clearTimeout(timeoutId);
}, [router, isAnonymous]);
return null; return null;
} }

View File

@ -9,16 +9,28 @@ export function useBrowserNavigation() {
const end = useAppTransitionStore(state => state.endNavigation); const end = useAppTransitionStore(state => state.endNavigation);
useEffect(() => { useEffect(() => {
let timeoutId: ReturnType<typeof setTimeout> | null = null;
const onPopState = () => { const onPopState = () => {
start(); start();
if (timeoutId) {
clearTimeout(timeoutId);
}
// Fallback to end the navigation in case route completes with cache // Fallback to end the navigation in case route completes with cache
setTimeout(() => { timeoutId = setTimeout(() => {
end(); end();
}, DELAY_CACHE_CHECK); // or cancel after Suspense/loader finishes timeoutId = null;
}, DELAY_CACHE_CHECK);
}; };
window.addEventListener('popstate', onPopState); window.addEventListener('popstate', onPopState);
return () => window.removeEventListener('popstate', onPopState); return () => {
window.removeEventListener('popstate', onPopState);
if (timeoutId) {
clearTimeout(timeoutId);
}
};
}, [start, end]); }, [start, end]);
} }