From 81af0794a778c489d91d5c46d9efbd1ab5cf0d19 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 23 Jan 2026 00:02:39 +0300 Subject: [PATCH] chore: debounce localStorage writes Signed-off-by: NotAShelf Change-Id: I26f31907405c2aa94f8127b4c9d4ac406a6a6964 --- src/index.tsx | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 04fbd86..a863ef1 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,6 +6,17 @@ import { StatsData, ComparisonEntry } from './utils/types'; import { parseStats } from './utils/formatters'; import './styles.css'; +function debounce) => ReturnType>( + fn: T, + delay: number, +): (...args: Parameters) => void { + let timeoutId: ReturnType; + return (...args: Parameters) => { + clearTimeout(timeoutId); + timeoutId = setTimeout(() => fn(...args), delay); + }; +} + const Analysis = lazy(() => import('./components/Analysis')); const ComparisonView = lazy(() => import('./components/ComparisonView')); @@ -64,6 +75,31 @@ function App() { }; }); + // Debounced save to localStorage + const saveToStorage = debounce( + ( + stats: StatsData | null, + raw: Record | null, + snaps: ComparisonEntry[], + v: 'analysis' | 'compare', + ) => { + try { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + snapshots: snaps, + currentStats: stats, + currentRaw: raw, + view: v, + }), + ); + } catch (e) { + console.warn('Failed to save data:', e); + } + }, + 500, + ); + // Save to localStorage on any change createEffect(() => { const stats = currentStats(); @@ -71,22 +107,9 @@ function App() { const snaps = snapshots(); const v = view(); - // Don't save while still loading initial data if (isLoading()) return; - try { - localStorage.setItem( - STORAGE_KEY, - JSON.stringify({ - snapshots: snaps, - currentStats: stats, - currentRaw: raw, - view: v, - }), - ); - } catch (e) { - console.warn('Failed to save data:', e); - } + saveToStorage(stats, raw, snaps, v); }); const saveSnapshot = () => {