-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlocalstorage.ts
More file actions
25 lines (23 loc) · 776 Bytes
/
localstorage.ts
File metadata and controls
25 lines (23 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Signal, createEffect, createRoot, createSignal } from 'solid-js';
import { Option, someIfNotNull } from '../types';
function createStorageSignal(
storage: Storage,
name: string,
): Signal<Option<string>> {
const [value, setValue] = createSignal<Option<string>>(
someIfNotNull(storage.getItem(name)),
);
// The session writer effect's lifecycle should be tied to that of the signal
createRoot(() => {
createEffect(() => {
value().match({
none: () => storage.removeItem(name),
some: (currentValue) => storage.setItem(name, currentValue),
});
});
});
return [value, setValue];
}
export function createLocalStorageSignal(name: string): Signal<Option<string>> {
return createStorageSignal(localStorage, name);
}