-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathstores.js
96 lines (76 loc) · 1.87 KB
/
stores.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
import {getCookie, setCookie, deleteCookie} from './utils/cookies'
export class TestStore {
constructor(){
this.value = null
}
get() {
return this.value
}
set(value) {
this.value = value;
}
delete() {
this.value = null
}
}
export class CookieStore {
constructor(manager) {
this.cookieName = manager.storageName
this.cookieDomain = manager.cookieDomain
this.cookiePath = manager.cookiePath
this.cookieExpiresAfterDays = manager.cookieExpiresAfterDays
}
get() {
const cookie = getCookie(this.cookieName);
return cookie
? cookie.value
: null;
}
set(value) {
return setCookie(this.cookieName, value, this.cookieExpiresAfterDays, this.cookieDomain, this.cookiePath)
}
delete() {
return deleteCookie(this.cookieName);
}
}
class StorageStore {
constructor(manager, handle) {
this.key = manager.storageName;
this.handle = handle
}
get() {
return this.handle.getItem(this.key);
}
getWithKey(key) {
return this.handle.getItem(key);
}
set(value) {
return this.handle.setItem(this.key, value)
}
setWithKey(key, value) {
return this.handle.setItem(key, value)
}
delete() {
return this.handle.removeItem(this.key);
}
deleteWithKey(key) {
return this.handle.removeItem(key);
}
}
export class LocalStorageStore extends StorageStore {
constructor(manager){
super(manager, localStorage)
}
}
export class SessionStorageStore extends StorageStore {
constructor(manager){
super(manager, sessionStorage)
}
}
const stores = {
'cookie': CookieStore,
'test': TestStore,
'localStorage': LocalStorageStore,
'sessionStorage': SessionStorageStore,
}
export default stores