-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorage.ts
46 lines (35 loc) · 911 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
36
37
38
39
40
41
42
43
44
45
46
class MemoryStorage {
private storage: Map<string, string>;
constructor() {
this.storage = new Map();
}
get length(): number {
return this.storage.size;
}
getItem(key: string): string | null {
const item = this.storage.get(key.toString());
return item === undefined ? null : item;
}
setItem(key: string, value: { toString: () => string }): void {
this.storage.set(key.toString(), value.toString());
}
removeItem(key: string): void {
this.storage.delete(key);
}
clear(): void {
this.storage.clear();
}
}
function storageService(): Storage | MemoryStorage {
const suffix = +new Date();
let storage;
try {
storage = window.localStorage;
storage.setItem(`__storage${suffix}`, '1');
storage.removeItem(`__storage${suffix}`);
} catch (e) {
storage = new MemoryStorage();
}
return storage;
}
export default storageService();