- commit
- fb0b016
- parent
- 1e24f21
- author
- Eric Bower
- date
- 2023-12-18 05:00:38 +0000 UTC
feat(store): add local storage adapter
1 files changed,
+24,
-0
+24,
-0
1@@ -20,6 +20,30 @@ export interface PersistProps<S extends AnyState> {
2 rehydrate: () => Operation<Result<unknown>>;
3 }
4
5+export function createLocalStorageAdapter<S extends AnyState>(): PersistAdapter<
6+ S
7+> {
8+ return {
9+ getItem: function* (key: string) {
10+ const storage = localStorage.getItem(key) || "{}";
11+ return Ok(JSON.parse(storage));
12+ },
13+ setItem: function* (key: string, s: Partial<S>) {
14+ const state = JSON.stringify(s);
15+ try {
16+ localStorage.setItem(key, state);
17+ } catch (err: any) {
18+ return Err(err);
19+ }
20+ return Ok(undefined);
21+ },
22+ removeItem: function* (key: string) {
23+ localStorage.removeItem(key);
24+ return Ok(undefined);
25+ },
26+ };
27+}
28+
29 export function shallowReconciler<S extends AnyState>(
30 original: S,
31 persisted: Partial<S>,