Skip to content

Commit 2058298

Browse files
juliusmarmingecodex
andcommitted
Bound repeated theme storage failures
Co-authored-by: codex <[email protected]>
1 parent ad8eb29 commit 2058298

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎apps/web/src/hooks/useTheme.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function createStorage(overrides: Partial<Storage> = {}): Storage {
2020
}
2121

2222
afterEach(() => {
23+
vi.doUnmock("react");
2324
vi.resetModules();
2425
vi.restoreAllMocks();
2526
vi.unstubAllGlobals();
@@ -101,6 +102,57 @@ describe("theme failure handling", () => {
101102
expect(JSON.stringify(attributes)).not.toContain(cause.message);
102103
});
103104

105+
it("retries a failed storage read only after a relevant storage event", async () => {
106+
const cause = new Error("persistent storage failure");
107+
const getItem = vi.fn(() => {
108+
throw cause;
109+
});
110+
const errorLog = vi.spyOn(console, "error").mockImplementation(() => {});
111+
let readSnapshot: (() => unknown) | undefined;
112+
let subscribeToTheme: ((listener: () => void) => () => void) | undefined;
113+
let storageHandler: ((event: StorageEvent) => void) | undefined;
114+
vi.doMock("react", () => ({
115+
useCallback: <A>(callback: A) => callback,
116+
useEffect: () => undefined,
117+
useSyncExternalStore: (
118+
subscribe: (listener: () => void) => () => void,
119+
getSnapshot: () => unknown,
120+
) => {
121+
subscribeToTheme = subscribe;
122+
readSnapshot = getSnapshot;
123+
return getSnapshot();
124+
},
125+
}));
126+
vi.stubGlobal("window", {
127+
addEventListener: (type: string, listener: (event: StorageEvent) => void) => {
128+
if (type === "storage") storageHandler = listener;
129+
},
130+
localStorage: createStorage({ getItem }),
131+
matchMedia: () => ({
132+
matches: false,
133+
addEventListener: () => undefined,
134+
removeEventListener: () => undefined,
135+
}),
136+
removeEventListener: () => undefined,
137+
});
138+
139+
const { useTheme } = await import("./useTheme");
140+
useTheme();
141+
readSnapshot?.();
142+
readSnapshot?.();
143+
144+
expect(getItem).toHaveBeenCalledTimes(1);
145+
expect(errorLog).toHaveBeenCalledTimes(1);
146+
147+
const unsubscribe = subscribeToTheme?.(() => undefined);
148+
storageHandler?.({ key: "t3code:theme" } as StorageEvent);
149+
readSnapshot?.();
150+
151+
expect(getItem).toHaveBeenCalledTimes(2);
152+
expect(errorLog).toHaveBeenCalledTimes(2);
153+
unsubscribe?.();
154+
});
155+
104156
it("preserves desktop sync causes and retries after a failed cosmetic sync", async () => {
105157
const cause = new Error("desktop IPC unavailable");
106158
const errorLog = vi.spyOn(console, "error").mockImplementation(() => {});

‎apps/web/src/hooks/useTheme.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ let listeners: Array<() => void> = [];
5555
let lastSnapshot: ThemeSnapshot | null = null;
5656
let lastDesktopTheme: Theme | null = null;
5757
let lastAppliedTheme: ThemeSnapshot | null = null;
58+
let themeStorageReadFailure: ThemeStorageError | null = null;
5859

5960
function emitChange() {
6061
for (const listener of listeners) listener();
@@ -88,6 +89,7 @@ export function writeThemePreference(theme: Theme): void {
8889
if (typeof window === "undefined") return;
8990
try {
9091
window.localStorage.setItem(STORAGE_KEY, theme);
92+
themeStorageReadFailure = null;
9193
} catch (cause) {
9294
throw new ThemeStorageError({
9395
operation: "write",
@@ -99,6 +101,9 @@ export function writeThemePreference(theme: Theme): void {
99101
}
100102

101103
function getStored(): Theme {
104+
if (themeStorageReadFailure !== null) {
105+
return DEFAULT_THEME_SNAPSHOT.theme;
106+
}
102107
try {
103108
return readThemePreference();
104109
} catch (cause) {
@@ -109,6 +114,7 @@ function getStored(): Theme {
109114
storageKey: STORAGE_KEY,
110115
cause,
111116
});
117+
themeStorageReadFailure = error;
112118
console.error(error.message, {
113119
operation: error.operation,
114120
storageKey: error.storageKey,
@@ -263,6 +269,7 @@ function subscribe(listener: () => void): () => void {
263269
// Listen for storage changes from other tabs
264270
const handleStorage = (e: StorageEvent) => {
265271
if (e.key === STORAGE_KEY) {
272+
themeStorageReadFailure = null;
266273
applyTheme(getStored(), true);
267274
emitChange();
268275
}

0 commit comments

Comments
 (0)