-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
202 lines (176 loc) · 4.62 KB
/
index.js
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
import { atom, map, onMount } from 'nanostores'
let identity = a => a
let storageEngine = {}
let eventsEngine = { addEventListener() {}, removeEventListener() {} }
function testSupport() {
try {
return typeof localStorage !== 'undefined'
/* c8 ignore next 4 */
} catch {
// In Privacy Mode access to localStorage will return error
return false
}
}
if (testSupport()) {
storageEngine = localStorage
}
export let windowPersistentEvents = {
addEventListener(key, listener, restore) {
window.addEventListener('storage', listener)
window.addEventListener('pageshow', restore)
},
removeEventListener(key, listener, restore) {
window.removeEventListener('storage', listener)
window.removeEventListener('pageshow', restore)
}
}
if (typeof window !== 'undefined') {
eventsEngine = windowPersistentEvents
}
export function setPersistentEngine(storage, events) {
storageEngine = storage
eventsEngine = events
}
export function persistentAtom(name, initial = undefined, opts = {}) {
let encode = opts.encode || identity
let decode = opts.decode || identity
let store = atom(initial)
let set = store.set
store.set = newValue => {
if (typeof newValue === 'undefined') {
delete storageEngine[name]
} else {
storageEngine[name] = encode(newValue)
}
set(newValue)
}
function listener(e) {
if (e.key === name) {
if (e.newValue === null) {
set(undefined)
} else {
set(decode(e.newValue))
}
} else if (!storageEngine[name]) {
set(undefined)
}
}
function restore() {
store.set(storageEngine[name] ? decode(storageEngine[name]) : initial)
}
onMount(store, () => {
restore()
if (opts.listen !== false) {
eventsEngine.addEventListener(name, listener, restore)
return () => {
eventsEngine.removeEventListener(name, listener, restore)
}
}
})
return store
}
export function persistentMap(prefix, initial = {}, opts = {}) {
let encode = opts.encode || identity
let decode = opts.decode || identity
let store = map()
let setKey = store.setKey
let storeKey = (key, newValue) => {
if (typeof newValue === 'undefined') {
if (opts.listen !== false && eventsEngine.perKey) {
eventsEngine.removeEventListener(prefix + key, listener, restore)
}
delete storageEngine[prefix + key]
} else {
if (
opts.listen !== false &&
eventsEngine.perKey &&
!(key in store.value)
) {
eventsEngine.addEventListener(prefix + key, listener, restore)
}
storageEngine[prefix + key] = encode(newValue)
}
}
store.setKey = (key, newValue) => {
storeKey(key, newValue)
setKey(key, newValue)
}
let set = store.set
store.set = function (newObject) {
for (let key in newObject) {
storeKey(key, newObject[key])
}
for (let key in store.value) {
if (!(key in newObject)) {
storeKey(key, undefined)
}
}
set(newObject)
}
function listener(e) {
if (!e.key) {
set({})
} else if (e.key.startsWith(prefix)) {
if (e.newValue === null) {
setKey(e.key.slice(prefix.length), undefined)
} else {
setKey(e.key.slice(prefix.length), decode(e.newValue))
}
}
}
function restore() {
let data = { ...initial }
for (let key in storageEngine) {
if (key.startsWith(prefix)) {
data[key.slice(prefix.length)] = decode(storageEngine[key])
}
}
for (let key in data) {
store.setKey(key, data[key])
}
}
onMount(store, () => {
restore()
if (opts.listen !== false) {
eventsEngine.addEventListener(prefix, listener, restore)
return () => {
eventsEngine.removeEventListener(prefix, listener, restore)
for (let key in store.value) {
eventsEngine.removeEventListener(prefix + key, listener, restore)
}
}
}
})
return store
}
let testStorage = {}
let testListeners = []
export function useTestStorageEngine() {
setPersistentEngine(testStorage, {
addEventListener(key, cb) {
testListeners.push(cb)
},
removeEventListener(key, cb) {
testListeners = testListeners.filter(i => i !== cb)
}
})
}
export function setTestStorageKey(key, newValue) {
if (typeof newValue === 'undefined') {
delete testStorage[key]
} else {
testStorage[key] = newValue
}
let event = { key, newValue }
for (let listener of testListeners) {
listener(event)
}
}
export function getTestStorage() {
return testStorage
}
export function cleanTestStorage() {
for (let i in testStorage) {
setTestStorageKey(i, undefined)
}
}