add manual garbage collection

This commit is contained in:
jeffvli
2026-01-01 12:21:05 -08:00
parent a7d2a427ec
commit 22c0f8f8c6
4 changed files with 67 additions and 1 deletions
@@ -0,0 +1,42 @@
import isElectron from 'is-electron';
import { useEffect, useRef } from 'react';
import { useLocation } from 'react-router';
const GARBAGE_COLLECTION_INTERVAL = 1000 * 60 * 5;
export const useGarbageCollection = () => {
const intervalIdRef = useRef<NodeJS.Timeout | null>(null);
// Clear the cache on an interval
useEffect(() => {
if (!isElectron()) {
return;
}
intervalIdRef.current = setInterval(() => {
window.api?.utils?.forceGarbageCollection?.();
}, GARBAGE_COLLECTION_INTERVAL);
return () => {
if (intervalIdRef.current) {
clearInterval(intervalIdRef.current);
}
};
}, []);
const location = useLocation();
// Clear the cache when the location changes
useEffect(() => {
if (!isElectron()) {
return;
}
// Clear the interval when the location changes
if (intervalIdRef.current) {
clearInterval(intervalIdRef.current);
}
window.api?.utils?.forceGarbageCollection?.();
}, [location]);
};