-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate-store.ts
41 lines (34 loc) · 1016 Bytes
/
state-store.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
export class StateStore {
private readonly _prefix: string;
private readonly _store: Storage;
constructor(args: { prefix: string; store: Storage } = { prefix: 'posc.', store: sessionStorage }) {
this._prefix = args.prefix;
this._store = args.store;
}
set(key: string, value: string) {
key = this._prefix + key;
this._store.setItem(key, value);
return Promise.resolve();
}
get(key: string): Promise<string> {
key = this._prefix + key;
let item = this._store.getItem(key);
return Promise.resolve(item);
}
remove(key: string) {
key = this._prefix + key;
let item = this._store.getItem(key);
this._store.removeItem(key);
return Promise.resolve(item);
}
getAllKeys() {
const keys = [];
for (let index = 0; index < this._store.length; index++) {
let key = this._store.key(index);
if (key.indexOf(this._prefix) === 0) {
keys.push(key.substr(this._prefix.length));
}
}
return Promise.resolve(keys);
}
}