GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(web): reload local storage subscribers when another tab clears st… · pingdotgg/t3code@8c04ef3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/web/src/hooks
    • useLocalStorage.ts
    • useSettings.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: apps/web/src/hooks/useLocalStorage.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ export function readLocalStorageValue<T, E>(
134134
/** Fires on writes from this tab and from other tabs. */
135135
export function subscribeLocalStorageKey(key: string, onChange: () => void): () => void {
136136
const handleStorageChange = (event: StorageEvent) => {
137-
if (event.key === key) {
137+
// Storage events carry a null key after clear().
138+
if (event.key === key || event.key === null) {
138139
onChange();
139140
}
140141
};
Collapse file

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

Copy file name to clipboardExpand all lines: apps/web/src/hooks/useSettings.test.ts
+25-12Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@ afterEach(() => {
3838
const otherTabSettings = { ...DEFAULT_CLIENT_SETTINGS, timestampFormat: "12-hour" as const };
3939
const readFailure = new Error("storage unavailable");
4040

41-
async function saveInOtherTab(failedReads = 0, saved: ClientSettings | null = otherTabSettings) {
41+
async function saveInOtherTab(
42+
failedReads = 0,
43+
saved: ClientSettings | null = otherTabSettings,
44+
key: string | null = "t3code:client-settings:v1",
45+
) {
4246
vi.stubGlobal("window", new EventTarget());
4347
await ensureClientSettingsHydrated();
4448
for (let read = 0; read < failedReads; read += 1) {
4549
persistenceMocks.getClientSettings.mockRejectedValueOnce(readFailure);
4650
}
4751
persistenceMocks.getClientSettings.mockResolvedValue(saved);
48-
window.dispatchEvent(Object.assign(new Event("storage"), { key: "t3code:client-settings:v1" }));
52+
window.dispatchEvent(Object.assign(new Event("storage"), { key }));
4953
}
5054

5155
describe("client settings hydration", () => {
@@ -214,16 +218,25 @@ describe("persistClientSettingsPatch", () => {
214218
it.each([
215219
{ change: "saved", loaded: null, saved: otherTabSettings },
216220
{ change: "cleared", loaded: otherTabSettings, saved: null },
217-
])("follows settings another tab $change after this tab loaded", async ({ loaded, saved }) => {
218-
persistenceMocks.getClientSettings.mockResolvedValueOnce(loaded);
219-
await saveInOtherTab(0, saved);
220-
221-
await persistClientSettingsPatch({ wordWrap: false });
222-
223-
const expected = { ...(saved ?? DEFAULT_CLIENT_SETTINGS), wordWrap: false };
224-
expect(persistenceMocks.setClientSettings).toHaveBeenLastCalledWith(expected);
225-
expect(getClientSettings()).toEqual(expected);
226-
});
221+
{
222+
change: "cleared with localStorage.clear()",
223+
loaded: otherTabSettings,
224+
saved: null,
225+
key: null,
226+
},
227+
])(
228+
"follows settings another tab $change after this tab loaded",
229+
async ({ loaded, saved, key }) => {
230+
persistenceMocks.getClientSettings.mockResolvedValueOnce(loaded);
231+
await saveInOtherTab(0, saved, key);
232+
233+
await persistClientSettingsPatch({ wordWrap: false });
234+
235+
const expected = { ...(saved ?? DEFAULT_CLIENT_SETTINGS), wordWrap: false };
236+
expect(persistenceMocks.setClientSettings).toHaveBeenLastCalledWith(expected);
237+
expect(getClientSettings()).toEqual(expected);
238+
},
239+
);
227240

228241
it("writes only after re-reading settings another tab saved when the reload failed", async () => {
229242
vi.spyOn(console, "error").mockImplementation(() => undefined);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.