mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix: prevent memory leak from unbounded setInterval in use-time-filter
This commit is contained in:
@@ -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]);
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user