MCPcopy Create free account
hub / github.com/bytebase/bytebase / safeRead

Function safeRead

frontend/src/react/stores/sqlEditor/tab.ts:95–110  ·  view source on GitHub ↗
(
  key: string,
  parse: (raw: unknown) => T | undefined,
  fallback: T
)

Source from the content-addressed store, hash-verified

93}
94
95const safeRead = <T>(
96 key: string,
97 parse: (raw: unknown) => T | undefined,
98 fallback: T
99): T => {
100 if (typeof window === "undefined") return fallback;
101 try {
102 const raw = window.localStorage.getItem(key);
103 if (raw === null) return fallback;
104 const parsed = JSON.parse(raw);
105 const valid = parse(parsed);
106 return valid === undefined ? fallback : valid;
107 } catch {
108 return fallback;
109 }
110};
111
112const safeWrite = (key: string, value: unknown) => {
113 if (typeof window === "undefined") return;

Callers 1

readOpenTabsFunction · 0.70

Calls 1

parseFunction · 0.85

Tested by

no test coverage detected