cleanup: centralise localStorage try/catch in renderer-shared — safeLocalStorageGet/Set/Remove
Every renderer module that persists state was wrapping its localStorage.getItem/setItem/removeItem call in the same try/catch idiom — handling private-browsing quirks and other sandbox contexts where storage isn't writable. Three identical patterns repeated nine times across renderer-streamers (filter / sort / hide-downloaded state), renderer-updates (skipped-update version), and renderer.ts (active-tab persistence).
Introduced three helpers in renderer-shared.ts:
- safeLocalStorageGet(key, fallback = '') — wraps getItem with the try/catch + fallback
- safeLocalStorageSet(key, value) — wraps setItem
- safeLocalStorageRemove(key) — wraps removeItem (needed for clearSkippedUpdateVersion which actually deletes the entry rather than blanking it)
Refactored 9 callsites. Reduces the noise:before:
try { return localStorage.getItem(KEY) ?? ''; } catch { return ''; }
try { localStorage.setItem(KEY, value); } catch { /* localStorage may be unavailable */ }
after:
return safeLocalStorageGet(KEY);
safeLocalStorageSet(KEY, value);
Left the VOD scroll-positions persistence in renderer-streamers untouched — its surrounding try/catch wraps JSON.parse/stringify logic that doesn't fit the simple helper signature.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
cf76e37c22
commit
752a5b2556
@ -19,6 +19,22 @@ function escapeHtml(value: string): string {
|
|||||||
.replace(/'/g, ''');
|
.replace(/'/g, ''');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* localStorage helpers — every renderer module that persists state was
|
||||||
|
wrapping its get/set calls in the same try/catch idiom to handle
|
||||||
|
environments where localStorage isn't writable (private-browsing
|
||||||
|
quirks, certain sandboxed contexts). Centralising the pattern. */
|
||||||
|
function safeLocalStorageGet(key: string, fallback = ''): string {
|
||||||
|
try { return localStorage.getItem(key) ?? fallback; } catch { return fallback; }
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeLocalStorageSet(key: string, value: string): void {
|
||||||
|
try { localStorage.setItem(key, value); } catch { /* localStorage may be unavailable */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeLocalStorageRemove(key: string): void {
|
||||||
|
try { localStorage.removeItem(key); } catch { /* localStorage may be unavailable */ }
|
||||||
|
}
|
||||||
|
|
||||||
let config: AppConfig = {};
|
let config: AppConfig = {};
|
||||||
let currentStreamer: string | null = null;
|
let currentStreamer: string | null = null;
|
||||||
let isConnected = false;
|
let isConnected = false;
|
||||||
|
|||||||
@ -53,11 +53,11 @@ const VOD_HIDE_DOWNLOADED_STORAGE_KEY = 'twitch-vod-manager:vod-hide-downloaded'
|
|||||||
let vodHideDownloaded = false;
|
let vodHideDownloaded = false;
|
||||||
|
|
||||||
function loadPersistedHideDownloaded(): boolean {
|
function loadPersistedHideDownloaded(): boolean {
|
||||||
try { return localStorage.getItem(VOD_HIDE_DOWNLOADED_STORAGE_KEY) === '1'; } catch { return false; }
|
return safeLocalStorageGet(VOD_HIDE_DOWNLOADED_STORAGE_KEY) === '1';
|
||||||
}
|
}
|
||||||
|
|
||||||
function persistHideDownloaded(value: boolean): void {
|
function persistHideDownloaded(value: boolean): void {
|
||||||
try { localStorage.setItem(VOD_HIDE_DOWNLOADED_STORAGE_KEY, value ? '1' : '0'); } catch { /* ignore */ }
|
safeLocalStorageSet(VOD_HIDE_DOWNLOADED_STORAGE_KEY, value ? '1' : '0');
|
||||||
}
|
}
|
||||||
|
|
||||||
function onVodHideDownloadedChange(): void {
|
function onVodHideDownloadedChange(): void {
|
||||||
@ -78,17 +78,15 @@ const VOD_SORT_STORAGE_KEY = 'twitch-vod-manager:vod-sort';
|
|||||||
let vodSortKey: VodSortKey = 'date_desc';
|
let vodSortKey: VodSortKey = 'date_desc';
|
||||||
|
|
||||||
function loadPersistedVodSort(): VodSortKey {
|
function loadPersistedVodSort(): VodSortKey {
|
||||||
try {
|
const stored = safeLocalStorageGet(VOD_SORT_STORAGE_KEY);
|
||||||
const stored = localStorage.getItem(VOD_SORT_STORAGE_KEY);
|
|
||||||
if (stored && (VALID_VOD_SORTS as readonly string[]).includes(stored)) {
|
if (stored && (VALID_VOD_SORTS as readonly string[]).includes(stored)) {
|
||||||
return stored as VodSortKey;
|
return stored as VodSortKey;
|
||||||
}
|
}
|
||||||
} catch { /* localStorage may be unavailable */ }
|
|
||||||
return 'date_desc';
|
return 'date_desc';
|
||||||
}
|
}
|
||||||
|
|
||||||
function persistVodSort(key: VodSortKey): void {
|
function persistVodSort(key: VodSortKey): void {
|
||||||
try { localStorage.setItem(VOD_SORT_STORAGE_KEY, key); } catch { /* localStorage may be unavailable */ }
|
safeLocalStorageSet(VOD_SORT_STORAGE_KEY, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
function vodDurationToSeconds(durationStr: string): number {
|
function vodDurationToSeconds(durationStr: string): number {
|
||||||
@ -162,15 +160,11 @@ function refreshVodSortSelectLabels(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function loadPersistedVodFilter(): string {
|
function loadPersistedVodFilter(): string {
|
||||||
try {
|
return safeLocalStorageGet(VOD_FILTER_STORAGE_KEY);
|
||||||
return localStorage.getItem(VOD_FILTER_STORAGE_KEY) ?? '';
|
|
||||||
} catch {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function persistVodFilter(query: string): void {
|
function persistVodFilter(query: string): void {
|
||||||
try { localStorage.setItem(VOD_FILTER_STORAGE_KEY, query); } catch { /* localStorage may be unavailable */ }
|
safeLocalStorageSet(VOD_FILTER_STORAGE_KEY, query);
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterVodsByQuery(vods: VOD[], query: string): VOD[] {
|
function filterVodsByQuery(vods: VOD[], query: string): VOD[] {
|
||||||
|
|||||||
@ -12,15 +12,15 @@ let shouldOpenUpdateModalOnAvailable = false;
|
|||||||
const SKIPPED_UPDATE_VERSION_KEY = 'twitch-vod-manager:skipped-update-version';
|
const SKIPPED_UPDATE_VERSION_KEY = 'twitch-vod-manager:skipped-update-version';
|
||||||
|
|
||||||
function getSkippedUpdateVersion(): string {
|
function getSkippedUpdateVersion(): string {
|
||||||
try { return localStorage.getItem(SKIPPED_UPDATE_VERSION_KEY) || ''; } catch { return ''; }
|
return safeLocalStorageGet(SKIPPED_UPDATE_VERSION_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
function persistSkippedUpdateVersion(version: string): void {
|
function persistSkippedUpdateVersion(version: string): void {
|
||||||
try { localStorage.setItem(SKIPPED_UPDATE_VERSION_KEY, version); } catch { /* localStorage may be unavailable */ }
|
safeLocalStorageSet(SKIPPED_UPDATE_VERSION_KEY, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearSkippedUpdateVersion(): void {
|
function clearSkippedUpdateVersion(): void {
|
||||||
try { localStorage.removeItem(SKIPPED_UPDATE_VERSION_KEY); } catch { /* localStorage may be unavailable */ }
|
safeLocalStorageRemove(SKIPPED_UPDATE_VERSION_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
function notifyUpdate(message: string, type: 'info' | 'warn' = 'info'): void {
|
function notifyUpdate(message: string, type: 'info' | 'warn' = 'info'): void {
|
||||||
|
|||||||
@ -836,16 +836,14 @@ function isKnownTab(value: string): value is typeof TAB_IDS[number] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function loadPersistedActiveTab(): string {
|
function loadPersistedActiveTab(): string {
|
||||||
try {
|
const stored = safeLocalStorageGet(ACTIVE_TAB_STORAGE_KEY);
|
||||||
const stored = localStorage.getItem(ACTIVE_TAB_STORAGE_KEY);
|
|
||||||
if (stored && isKnownTab(stored)) return stored;
|
if (stored && isKnownTab(stored)) return stored;
|
||||||
} catch { /* localStorage may be unavailable in privacy modes */ }
|
|
||||||
return 'vods';
|
return 'vods';
|
||||||
}
|
}
|
||||||
|
|
||||||
function persistActiveTab(tab: string): void {
|
function persistActiveTab(tab: string): void {
|
||||||
if (!isKnownTab(tab)) return;
|
if (!isKnownTab(tab)) return;
|
||||||
try { localStorage.setItem(ACTIVE_TAB_STORAGE_KEY, tab); } catch { }
|
safeLocalStorageSet(ACTIVE_TAB_STORAGE_KEY, tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
function showTab(tab: string): void {
|
function showTab(tab: string): void {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user