-
Notifications
You must be signed in to change notification settings - Fork 68
/
LocalStorage.tsx
65 lines (57 loc) · 2.24 KB
/
LocalStorage.tsx
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
/*
* Copyright (c) 2023 Discover Financial Services
* Licensed under Apache-2.0 License. See License.txt in the project root for license information
*/
import { Storage, StorageElement } from "@finos/a11y-theme-builder-sdk";
const path = "themebuilder-storage-";
export class LocalStorage implements Storage {
public async get(key: string): Promise<StorageElement> {
const response = window.localStorage.getItem(path+key);
//console.log(`LocalStorage.get(${key}) => ${response} type=${typeof response}`);
let r = {metadata:{}};
if (response) {
r = JSON.parse(response);
}
return r;
}
public async set(key: string, data: StorageElement) {
//console.log(`LocalStorage.set(${key}, ${value})`);
const keys = await this.listKeys();
if (keys.indexOf(key) > -1) {
const response = window.localStorage.getItem(path+key);
const existing = JSON.parse(response || "{}");
window.localStorage.setItem(path+key, JSON.stringify({...existing, ...data}));
//console.log(`set(${key}) PATCH`);
}
else {
window.localStorage.setItem(path+key, JSON.stringify(data));
//console.log(`set(${key}) POST`);
}
}
public async delete(key: string) {
//console.log(`LocalStorage.delete(${key})}`);
window.localStorage.removeItem(path+key);
}
public async listKeys(): Promise<string[]> {
//console.log(`LocalStorage.listKeys()`);
let keys = Object.keys(window.localStorage);
const data = [];
const start = path.length;
for (var i in keys) {
//console.log(keys[i]+"="+localStorage.getItem(keys[i]))
if (keys[i].startsWith(path)) {
data.push(keys[i].substring(start));
}
}
return data;
}
public async listMetadata(key?: string): Promise<StorageElement[]> {
const metadata = [];
const keys = await this.listKeys();
for (var i=0; i<keys.length; i++) {
const item = await this.get(keys[i]) as any;
metadata.push({id: keys[i], metadata: item.metadata});
}
return metadata;
}
}