deno.land / x / jotai@v1.8.4 / src / utils / atomWithStorage.ts

atomWithStorage.ts
نووسراو ببینە
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { atom } from 'jotai'import type { WritableAtom } from 'jotai'import { RESET } from './constants'
export const NO_STORAGE_VALUE = Symbol()
type Unsubscribe = () => void
type SetStateActionWithReset<Value> = | Value | typeof RESET | ((prev: Value) => Value | typeof RESET)
export interface AsyncStorage<Value> { getItem: (key: string) => Promise<Value | typeof NO_STORAGE_VALUE> setItem: (key: string, newValue: Value) => Promise<void> removeItem: (key: string) => Promise<void> delayInit?: boolean subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe}
export interface SyncStorage<Value> { getItem: (key: string) => Value | typeof NO_STORAGE_VALUE setItem: (key: string, newValue: Value) => void removeItem: (key: string) => void delayInit?: boolean subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe}
export interface AsyncStringStorage { getItem: (key: string) => Promise<string | null> setItem: (key: string, newValue: string) => Promise<void> removeItem: (key: string) => Promise<void>}
export interface SyncStringStorage { getItem: (key: string) => string | null setItem: (key: string, newValue: string) => void removeItem: (key: string) => void}
export function createJSONStorage<Value>( getStringStorage: () => AsyncStringStorage): AsyncStorage<Value>
export function createJSONStorage<Value>( getStringStorage: () => SyncStringStorage): SyncStorage<Value>
export function createJSONStorage<Value>( getStringStorage: () => AsyncStringStorage | SyncStringStorage | undefined): AsyncStorage<Value> | SyncStorage<Value> { let lastStr: string | undefined let lastValue: any const storage: AsyncStorage<Value> | SyncStorage<Value> = { getItem: (key) => { const parse = (str: string | null) => { str = str || '' if (lastStr !== str) { try { lastValue = JSON.parse(str) } catch { return NO_STORAGE_VALUE } lastStr = str } return lastValue } const str = getStringStorage()?.getItem(key) ?? null if (str instanceof Promise) { return str.then(parse) } return parse(str) }, setItem: (key, newValue) => getStringStorage()?.setItem(key, JSON.stringify(newValue)), removeItem: (key) => getStringStorage()?.removeItem(key), } if ( typeof window !== 'undefined' && typeof window.addEventListener === 'function' ) { storage.subscribe = (key, callback) => { const storageEventCallback = (e: StorageEvent) => { if (e.key === key && e.newValue) { callback(JSON.parse(e.newValue)) } } window.addEventListener('storage', storageEventCallback) return () => { window.removeEventListener('storage', storageEventCallback) } } } return storage}
const defaultStorage = createJSONStorage(() => typeof window !== 'undefined' ? window.localStorage : (undefined as unknown as Storage))
export function atomWithStorage<Value>( key: string, initialValue: Value, storage: AsyncStorage<Value> & { delayInit: true }): WritableAtom<Value, SetStateActionWithReset<Value>, Promise<void>>
export function atomWithStorage<Value>( key: string, initialValue: Value, storage: AsyncStorage<Value>): WritableAtom<Promise<Value>, SetStateActionWithReset<Value>, Promise<void>>
export function atomWithStorage<Value>( key: string, initialValue: Value, storage: SyncStorage<Value>): WritableAtom<Value, SetStateActionWithReset<Value>>
export function atomWithStorage<Value>( key: string, initialValue: Value): WritableAtom<Value, SetStateActionWithReset<Value>>
export function atomWithStorage<Value>( key: string, initialValue: Value, storage: | SyncStorage<Value> | AsyncStorage<Value> = defaultStorage as SyncStorage<Value>) { const getInitialValue = () => { const value = storage.getItem(key) if (value instanceof Promise) { return value.then((v) => (v === NO_STORAGE_VALUE ? initialValue : v)) } return value === NO_STORAGE_VALUE ? initialValue : value }
const baseAtom = atom(storage.delayInit ? initialValue : getInitialValue())
baseAtom.onMount = (setAtom) => { let unsub: Unsubscribe | undefined if (storage.subscribe) { unsub = storage.subscribe(key, setAtom) // in case it's updated before subscribing setAtom(getInitialValue()) } if (storage.delayInit) { const value = getInitialValue() if (value instanceof Promise) { value.then(setAtom) } else { setAtom(value) } } return unsub }
const anAtom = atom( (get) => get(baseAtom), (get, set, update: SetStateActionWithReset<Value>) => { const nextValue = typeof update === 'function' ? (update as (prev: Value) => Value | typeof RESET)(get(baseAtom)) : update
if (nextValue === RESET) { set(baseAtom, initialValue) return storage.removeItem(key) }
set(baseAtom, nextValue) return storage.setItem(key, nextValue) } )
return anAtom}
// atomWithHash is implemented with atomWithStorage
export function atomWithHash<Value>( key: string, initialValue: Value, options?: { serialize?: (val: Value) => string deserialize?: (str: string | null) => Value | typeof NO_STORAGE_VALUE delayInit?: boolean replaceState?: boolean subscribe?: (callback: () => void) => () => void }): WritableAtom<Value, SetStateActionWithReset<Value>> { const serialize = options?.serialize || JSON.stringify const deserialize = options?.deserialize || ((str) => { try { return JSON.parse(str || '') } catch { return NO_STORAGE_VALUE } }) const subscribe = options?.subscribe || ((callback) => { window.addEventListener('hashchange', callback) return () => { window.removeEventListener('hashchange', callback) } }) const hashStorage: SyncStorage<Value> = { getItem: (key) => { if (typeof location === 'undefined') { return NO_STORAGE_VALUE } const searchParams = new URLSearchParams(location.hash.slice(1)) const storedValue = searchParams.get(key) return deserialize(storedValue) }, setItem: (key, newValue) => { const searchParams = new URLSearchParams(location.hash.slice(1)) searchParams.set(key, serialize(newValue)) if (options?.replaceState) { history.replaceState(null, '', '#' + searchParams.toString()) } else { location.hash = searchParams.toString() } }, removeItem: (key) => { const searchParams = new URLSearchParams(location.hash.slice(1)) searchParams.delete(key) if (options?.replaceState) { history.replaceState(null, '', '#' + searchParams.toString()) } else { location.hash = searchParams.toString() } }, ...(options?.delayInit && { delayInit: true }), subscribe: (key, setValue) => { const callback = () => { const searchParams = new URLSearchParams(location.hash.slice(1)) const str = searchParams.get(key) if (str !== null) { setValue(deserialize(str)) } else { setValue(initialValue) } } return subscribe(callback) }, }
return atomWithStorage(key, initialValue, hashStorage)}
jotai

Version Info

Tagged at
2 years ago