-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcached.ts
71 lines (60 loc) · 1.62 KB
/
cached.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
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
import KeyValueStorage from '@walletconnect/keyvaluestorage';
export interface Data {
address: string;
brandName: string;
chainId?: number;
silent?: boolean;
status?: number;
sessionStatus?: string;
networkDelay?: number;
namespace?: string;
deepLink: string;
}
const storage = new KeyValueStorage();
export class Cached {
topics: Map<string, Data> = new Map();
constructor() {
storage.getItem('wc_topics').then((topics) => {
if (topics) {
const topicsArr =
typeof topics === 'object' ? topics : JSON.parse(topics);
this.topics = new Map(topicsArr);
}
});
}
getTopic(topic: string) {
return this.topics.get(topic);
}
setTopic(topic: string, data: Data) {
this.topics.set(topic, data);
storage.setItem('wc_topics', JSON.stringify(Array.from(this.topics)));
}
deleteTopic(topic: string) {
this.topics.delete(topic);
storage.setItem('wc_topics', JSON.stringify(Array.from(this.topics)));
}
updateTopic(topic: string, data: Partial<Data>) {
if (this.topics.has(topic)) {
this.setTopic(topic, {
...this.getTopic(topic),
...data
} as Data);
}
}
findTopic(data: Partial<Data>): string | undefined {
const { address, brandName, chainId } = data;
const keys = this.topics.keys();
for (const key of keys) {
const value = this.getTopic(key);
if (
value?.address.toLowerCase() === address?.toLowerCase() &&
value?.brandName.toLowerCase() === brandName?.toLowerCase()
) {
return key;
}
}
}
getAllTopics() {
return [...this.topics.keys()];
}
}