forked from weaigc/bingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.ts
35 lines (34 loc) · 945 Bytes
/
storage.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
// @ts-ignore
import nameStorage from 'namestorage'
export default () => {
return {
getItem(key: string) {
return JSON.parse(nameStorage.getItem(key))
},
setItem(key: string, value: string) {
const event = new CustomEvent("namestorage", {
detail: {
key,
value,
},
});
window.dispatchEvent(event)
nameStorage.setItem(key, JSON.stringify(value))
},
removeItem(key: string) {
nameStorage.removeItem(key)
},
subscribe(okey: string, callback: (value: any) => void) {
const storageEventCallback = (event: any) => {
const { key, value } = event.detail as { key: string, value: any }
if (key === okey) {
callback(value)
}
}
window.addEventListener('namestorage', storageEventCallback)
return () => {
window.removeEventListener('namestorage', storageEventCallback)
}
}
}
}