fix: prevent memory leak from unbounded setInterval in use-time-filter

This commit is contained in:
plebeius
2025-11-06 11:23:06 +01:00
parent cc88d805f4
commit eedb0fa08e
2 changed files with 19 additions and 6 deletions
@@ -194,9 +194,11 @@ const MobileQuotePreview = ({ backlinkReply, quotelinkReply, isBacklinkReply, is
});
useEffect(() => {
window.addEventListener('resize', () => update());
// Create a stable function reference for proper cleanup
const handleResize = () => update();
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', () => update());
window.removeEventListener('resize', handleResize);
};
}, [update]);
+15 -4
View File
@@ -4,10 +4,21 @@ import { useParams } from 'react-router-dom';
// the timestamp the last time the user visited
const lastVisitTimestamp = localStorage.getItem('5chanLastVisitTimestamp');
// update the last visited timestamp every n seconds
setInterval(() => {
localStorage.setItem('5chanLastVisitTimestamp', Date.now().toString());
}, 60 * 1000);
// Singleton pattern to ensure only one interval is ever created
// This prevents memory leaks from multiple intervals being created during HMR or module re-evaluation
// Using window object to persist across hot module reloads
declare global {
interface Window {
_5chanLastVisitTimestampIntervalId?: number;
}
}
if (!window._5chanLastVisitTimestampIntervalId) {
// update the last visited timestamp every n seconds
window._5chanLastVisitTimestampIntervalId = window.setInterval(() => {
localStorage.setItem('5chanLastVisitTimestamp', Date.now().toString());
}, 60 * 1000);
}
const timeFilterNamesToSeconds: Record<string, number | undefined> = {
'1h': 60 * 60,