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(() => { useEffect(() => {
window.addEventListener('resize', () => update()); // Create a stable function reference for proper cleanup
const handleResize = () => update();
window.addEventListener('resize', handleResize);
return () => { return () => {
window.removeEventListener('resize', () => update()); window.removeEventListener('resize', handleResize);
}; };
}, [update]); }, [update]);
+15 -4
View File
@@ -4,10 +4,21 @@ import { useParams } from 'react-router-dom';
// the timestamp the last time the user visited // the timestamp the last time the user visited
const lastVisitTimestamp = localStorage.getItem('5chanLastVisitTimestamp'); const lastVisitTimestamp = localStorage.getItem('5chanLastVisitTimestamp');
// update the last visited timestamp every n seconds // Singleton pattern to ensure only one interval is ever created
setInterval(() => { // This prevents memory leaks from multiple intervals being created during HMR or module re-evaluation
localStorage.setItem('5chanLastVisitTimestamp', Date.now().toString()); // Using window object to persist across hot module reloads
}, 60 * 1000); 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> = { const timeFilterNamesToSeconds: Record<string, number | undefined> = {
'1h': 60 * 60, '1h': 60 * 60,