@devries-cameron20 / t3code Mirror · read-only / commits / f5e4df5cab
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 25, 08:45 UTC

Use Debouncer for store state persistence

- Replace manual timeout debounce logic with `@tanstack/react-pacer`'s `Debouncer` - Persist updates via `maybeExecute` to reduce localStorage write thrashing - Flush pending persistence on `beforeunload` to avoid losing recent state

Julius Marminge committed Mar 9, 2026 at 12:28 UTC f5e4df5cab247021501876358fd72c7196cd96b9
1 file changed +4 -18
apps/web/src/store.ts
+4 -18
@@ -14,6 +14,7 @@ import {
14 } from "@t3tools/shared/model";
15 import { create } from "zustand";
16 import { type ChatMessage, type Project, type Thread } from "./types";
17 +import { Debouncer } from "@tanstack/react-pacer";
18
19 // ── State ────────────────────────────────────────────────────────────
20
@@ -86,18 +87,7 @@ function persistState(state: AppState): void {
87 // Ignore quota/storage errors to avoid breaking chat UX.
88 }
89 }
89 -
90 -let persistTimer: ReturnType<typeof setTimeout> | null = null;
91 -
92 -function debouncedPersistState(state: AppState): void {
93 - if (persistTimer !== null) {
94 - clearTimeout(persistTimer);
95 - }
96 - persistTimer = setTimeout(() => {
97 - persistTimer = null;
98 - persistState(state);
99 - }, 500);
100 -}
90 +const debouncedPersistState = new Debouncer(persistState, { wait: 500 });
91
92 // ── Pure helpers ──────────────────────────────────────────────────────
93
@@ -413,16 +403,12 @@ export const useStore = create<AppStore>((set) => ({
403 }));
404
405 // Persist state changes with debouncing to avoid localStorage thrashing
416 -useStore.subscribe((state) => debouncedPersistState(state));
406 +useStore.subscribe((state) => debouncedPersistState.maybeExecute(state));
407
408 // Flush pending writes synchronously before page unload to prevent data loss.
409 if (typeof window !== "undefined") {
410 window.addEventListener("beforeunload", () => {
421 - if (persistTimer !== null) {
422 - clearTimeout(persistTimer);
423 - persistTimer = null;
424 - persistState(useStore.getState());
425 - }
411 + debouncedPersistState.flush();
412 });
413 }
414