chore: debounce localStorage writes

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I26f31907405c2aa94f8127b4c9d4ac406a6a6964
This commit is contained in:
raf 2026-01-23 00:02:39 +03:00
commit 81af0794a7
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -6,6 +6,17 @@ import { StatsData, ComparisonEntry } from './utils/types';
import { parseStats } from './utils/formatters'; import { parseStats } from './utils/formatters';
import './styles.css'; import './styles.css';
function debounce<T extends (...args: Parameters<T>) => ReturnType<T>>(
fn: T,
delay: number,
): (...args: Parameters<T>) => void {
let timeoutId: ReturnType<typeof setTimeout>;
return (...args: Parameters<T>) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn(...args), delay);
};
}
const Analysis = lazy(() => import('./components/Analysis')); const Analysis = lazy(() => import('./components/Analysis'));
const ComparisonView = lazy(() => import('./components/ComparisonView')); const ComparisonView = lazy(() => import('./components/ComparisonView'));
@ -64,16 +75,14 @@ function App() {
}; };
}); });
// Save to localStorage on any change // Debounced save to localStorage
createEffect(() => { const saveToStorage = debounce(
const stats = currentStats(); (
const raw = currentRaw(); stats: StatsData | null,
const snaps = snapshots(); raw: Record<string, unknown> | null,
const v = view(); snaps: ComparisonEntry[],
v: 'analysis' | 'compare',
// Don't save while still loading initial data ) => {
if (isLoading()) return;
try { try {
localStorage.setItem( localStorage.setItem(
STORAGE_KEY, STORAGE_KEY,
@ -87,6 +96,20 @@ function App() {
} catch (e) { } catch (e) {
console.warn('Failed to save data:', e); console.warn('Failed to save data:', e);
} }
},
500,
);
// Save to localStorage on any change
createEffect(() => {
const stats = currentStats();
const raw = currentRaw();
const snaps = snapshots();
const v = view();
if (isLoading()) return;
saveToStorage(stats, raw, snaps, v);
}); });
const saveSnapshot = () => { const saveSnapshot = () => {