-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.js
More file actions
168 lines (154 loc) · 4.59 KB
/
Copy pathstate.js
File metadata and controls
168 lines (154 loc) · 4.59 KB
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
export class State extends EventTarget {
// implements EventTarget (partially anyways) https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
static statePrefix = 'state.'
constructor(options = {}) {
super()
this.options = options
this.storage = options.storage || 'local'
if (this.storage === 'local') {
this.storageObj = typeof window !== 'undefined' ? window.localStorage : null
} else if (this.storage === 'session') {
this.storageObj = typeof window !== 'undefined' ? window.sessionStorage : null
} else if (this.storage === 'memory') {
this.storageObj = null
} else {
this.storageObj = this.storage
}
this.stateMap = new Map()
this.ttlMap = new Map()
this.loadState()
if (typeof window !== 'undefined') {
window.addEventListener('storage', (e) => {
if (e.storageArea && e.storageArea !== this.storageObj) {
return
}
if (e.key && e.key.startsWith(State.statePrefix)) {
// console.log("storage event", e)
let key = e.key.substring(State.statePrefix.length)
let value = null
let expiresAt = null
if (e.newValue) {
try {
const parsed = JSON.parse(e.newValue)
value = parsed?.value
expiresAt = parsed?.expiresAt
} catch (err) {
console.log(`[state] ${e.key} invalid json`)
}
}
if (e.newValue && (!expiresAt || Date.now() <= expiresAt)) {
this.stateMap.set(key, value)
if (expiresAt) {
this.ttlMap.set(key, expiresAt)
} else {
this.ttlMap.delete(key)
}
this.dispatchEvent(
new CustomEvent(key, {
detail: {
action: value ? 'set' : 'delete',
key,
value,
},
}),
)
} else {
const r = this.stateMap.delete(key)
this.ttlMap.delete(key)
this.dispatchEvent(
new CustomEvent(key, {
detail: {
action: 'delete',
key,
value: null,
deleted: r,
},
}),
)
}
}
})
}
}
loadState() {
if (!this.storageObj) return
for (const key in this.storageObj) {
if (key.startsWith(State.statePrefix)) {
let raw = this.storageObj.getItem(key)
try {
const parsed = JSON.parse(raw)
const value = parsed?.value
const expiresAt = parsed?.expiresAt
const stateKey = key.substring(State.statePrefix.length)
if (expiresAt && Date.now() > expiresAt) {
this.storageObj.removeItem(key)
} else {
this.stateMap.set(stateKey, value)
if (expiresAt) {
this.ttlMap.set(stateKey, expiresAt)
}
}
} catch (e) {
console.log(`[state] ${key} invalid json`)
}
}
}
}
/**
* Set the value of a state key.
* @param {string} key
* @param {any} value
* @param {object} opts
* @param {boolean} [opts.skipStorage=false] if true, the value will not be stored in storage
* @param {number} [opts.ttl] Time-To-Live in milliseconds
*/
set(key, value, opts = {}) {
const expiresAt = opts.ttl ? Date.now() + opts.ttl : null
const m = this.stateMap.set(key, value)
if (expiresAt) {
this.ttlMap.set(key, expiresAt)
} else {
this.ttlMap.delete(key)
}
if (!opts.skipStorage && this.storageObj) {
const wrapped = { value, expiresAt }
this.storageObj.setItem(`${State.statePrefix}${key}`, JSON.stringify(wrapped))
}
this.dispatchEvent(
new CustomEvent(key, {
detail: {
action: 'set',
key,
value,
},
}),
)
return m
}
delete(key) {
const r = this.stateMap.delete(key)
this.ttlMap.delete(key)
if (this.storageObj) {
this.storageObj.removeItem(`${State.statePrefix}${key}`)
}
this.dispatchEvent(
new CustomEvent(key, {
detail: {
action: 'delete',
key,
deleted: r, // true if element existed and was removed, or false if the element did not exist.
},
}),
)
}
get(key) {
const expiresAt = this.ttlMap.get(key)
if (expiresAt && Date.now() > expiresAt) {
this.delete(key)
return undefined
}
return this.stateMap.get(key)
}
}
export const state = new State()
export default state