|
1 | 1 | import * as Schema from "effect/Schema"; |
2 | 2 | import * as Record from "effect/Record"; |
3 | | -import { useCallback, useMemo, useSyncExternalStore } from "react"; |
| 3 | +import { useSyncExternalStore } from "react"; |
4 | 4 |
|
5 | 5 | export class LocalStorageOperationError extends Schema.TaggedError<LocalStorageOperationError>()( |
6 | 6 | "LocalStorageOperationError", |
@@ -97,93 +97,105 @@ function dispatchLocalStorageChange(key: string) { |
97 | 97 | } |
98 | 98 | } |
99 | 99 |
|
| 100 | +function readSerializedValue(key: string): string | null { |
| 101 | + try { |
| 102 | + return read(key); |
| 103 | + } catch (error) { |
| 104 | + console.error("[LOCALSTORAGE] Could not read stored value.", error); |
| 105 | + return null; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function decodeOrInitial<T, E>( |
| 110 | + key: string, |
| 111 | + schema: Schema.Codec<T, E>, |
| 112 | + serializedValue: string | null, |
| 113 | + initialValue: T, |
| 114 | +): T { |
| 115 | + if (serializedValue === null) { |
| 116 | + return initialValue; |
| 117 | + } |
| 118 | + try { |
| 119 | + return decode(key, schema, serializedValue); |
| 120 | + } catch (error) { |
| 121 | + console.error("[LOCALSTORAGE] Could not decode stored value.", error); |
| 122 | + return initialValue; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function writeLocalStorageValue<T, E>( |
| 127 | + key: string, |
| 128 | + value: T | ((val: T) => T), |
| 129 | + initialValue: T, |
| 130 | + schema: Schema.Codec<T, E>, |
| 131 | +): void { |
| 132 | + try { |
| 133 | + let valueToStore: T; |
| 134 | + if (typeof value === "function") { |
| 135 | + // Like the read path, treat an unreadable stored value as the initial |
| 136 | + // value, so a corrupt blob cannot block every later write. |
| 137 | + let currentValue = initialValue; |
| 138 | + try { |
| 139 | + currentValue = getLocalStorageItem(key, schema) ?? initialValue; |
| 140 | + } catch (error) { |
| 141 | + console.error("[LOCALSTORAGE] Could not read stored value.", error); |
| 142 | + } |
| 143 | + try { |
| 144 | + valueToStore = (value as (val: T) => T)(currentValue); |
| 145 | + } catch (cause) { |
| 146 | + throw new LocalStorageOperationError({ |
| 147 | + operation: "update", |
| 148 | + storageKey: key, |
| 149 | + cause, |
| 150 | + }); |
| 151 | + } |
| 152 | + } else { |
| 153 | + valueToStore = value; |
| 154 | + } |
| 155 | + if (valueToStore === null) { |
| 156 | + removeLocalStorageItem(key); |
| 157 | + } else { |
| 158 | + setLocalStorageItem(key, valueToStore, schema); |
| 159 | + } |
| 160 | + dispatchLocalStorageChange(key); |
| 161 | + } catch (error) { |
| 162 | + console.error("[LOCALSTORAGE] Could not update stored value.", error); |
| 163 | + } |
| 164 | +} |
| 165 | + |
100 | 166 | export function useLocalStorage<T, E>( |
101 | 167 | key: string, |
102 | 168 | initialValue: T, |
103 | 169 | schema: Schema.Codec<T, E>, |
104 | 170 | ): [T, (value: T | ((val: T) => T)) => void] { |
105 | | - const getSnapshot = useCallback(() => { |
106 | | - try { |
107 | | - return read(key); |
108 | | - } catch (error) { |
109 | | - console.error("[LOCALSTORAGE] Could not read stored value.", error); |
110 | | - return null; |
111 | | - } |
112 | | - }, [key]); |
113 | | - |
114 | | - const subscribe = useCallback( |
115 | | - (onStoreChange: () => void) => { |
116 | | - const handleStorageChange = (event: StorageEvent) => { |
117 | | - if (event.key === key) { |
118 | | - onStoreChange(); |
119 | | - } |
120 | | - }; |
121 | | - const handleLocalChange = (event: CustomEvent<LocalStorageChangeDetail>) => { |
122 | | - if (event.detail.key === key) { |
123 | | - onStoreChange(); |
124 | | - } |
125 | | - }; |
126 | | - |
127 | | - window.addEventListener("storage", handleStorageChange); |
128 | | - window.addEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener); |
129 | | - return () => { |
130 | | - window.removeEventListener("storage", handleStorageChange); |
131 | | - window.removeEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener); |
132 | | - }; |
133 | | - }, |
134 | | - [key], |
135 | | - ); |
| 171 | + const getSnapshot = () => readSerializedValue(key); |
| 172 | + |
| 173 | + const subscribe = (onStoreChange: () => void) => { |
| 174 | + const handleStorageChange = (event: StorageEvent) => { |
| 175 | + if (event.key === key) { |
| 176 | + onStoreChange(); |
| 177 | + } |
| 178 | + }; |
| 179 | + const handleLocalChange = (event: CustomEvent<LocalStorageChangeDetail>) => { |
| 180 | + if (event.detail.key === key) { |
| 181 | + onStoreChange(); |
| 182 | + } |
| 183 | + }; |
| 184 | + |
| 185 | + window.addEventListener("storage", handleStorageChange); |
| 186 | + window.addEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener); |
| 187 | + return () => { |
| 188 | + window.removeEventListener("storage", handleStorageChange); |
| 189 | + window.removeEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener); |
| 190 | + }; |
| 191 | + }; |
136 | 192 |
|
137 | 193 | const serializedValue = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); |
138 | | - const storedValue = useMemo(() => { |
139 | | - if (serializedValue === null) { |
140 | | - return initialValue; |
141 | | - } |
142 | | - try { |
143 | | - return decode(key, schema, serializedValue); |
144 | | - } catch (error) { |
145 | | - console.error("[LOCALSTORAGE] Could not decode stored value.", error); |
146 | | - return initialValue; |
147 | | - } |
148 | | - }, [initialValue, key, schema, serializedValue]); |
| 194 | + const storedValue = decodeOrInitial(key, schema, serializedValue, initialValue); |
149 | 195 |
|
150 | | - const setValue = useCallback( |
151 | | - (value: T | ((val: T) => T)) => { |
152 | | - try { |
153 | | - let valueToStore: T; |
154 | | - if (typeof value === "function") { |
155 | | - // Like the read path, treat an unreadable stored value as the initial |
156 | | - // value, so a corrupt blob cannot block every later write. |
157 | | - let currentValue = initialValue; |
158 | | - try { |
159 | | - currentValue = getLocalStorageItem(key, schema) ?? initialValue; |
160 | | - } catch (error) { |
161 | | - console.error("[LOCALSTORAGE] Could not read stored value.", error); |
162 | | - } |
163 | | - try { |
164 | | - valueToStore = (value as (val: T) => T)(currentValue); |
165 | | - } catch (cause) { |
166 | | - throw new LocalStorageOperationError({ |
167 | | - operation: "update", |
168 | | - storageKey: key, |
169 | | - cause, |
170 | | - }); |
171 | | - } |
172 | | - } else { |
173 | | - valueToStore = value; |
174 | | - } |
175 | | - if (valueToStore === null) { |
176 | | - removeLocalStorageItem(key); |
177 | | - } else { |
178 | | - setLocalStorageItem(key, valueToStore, schema); |
179 | | - } |
180 | | - dispatchLocalStorageChange(key); |
181 | | - } catch (error) { |
182 | | - console.error("[LOCALSTORAGE] Could not update stored value.", error); |
183 | | - } |
184 | | - }, |
185 | | - [initialValue, key, schema], |
186 | | - ); |
| 196 | + const setValue = (value: T | ((val: T) => T)) => { |
| 197 | + writeLocalStorageValue(key, value, initialValue, schema); |
| 198 | + }; |
187 | 199 |
|
188 | 200 | return [storedValue, setValue]; |
189 | 201 | } |
0 commit comments