-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
44 lines (44 loc) · 1022 Bytes
/
storage.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
class NetFlexStorage {
constructor() {
this.db = chrome.storage.sync;
this.allKeys = 'NetFlexKeys';
}
addToAllKeys(key) {
if(key != this.allKeys) {
this.add(this.allKeys, key);
}
}
getAllKeys(callback) {
this.readEach(this.allKeys, callback);
}
read(key, callback) {
this.db.get(key, data => callback(data[key] || []));
}
readEach(key, callback) {
this.read(key, list => list.forEach(callback));
}
write(key, value) {
let data = {};
data[key] = value;
this.db.set(data);
this.addToAllKeys(key);
}
writeList(key, list) {
this.write(key, Array.from(new Set(list.sort())));
}
add(key, value) {
this.read(key, list => {
list.push(value);
this.writeList(key, list);
});
}
remove(key, value) {
this.read(key, list => {
list.splice(list.indexOf(value), 1);
this.writeList(key, list);
});
}
toggle(k, v) {
this.read(k, list => list.includes(v) ? this.remove(k, v) : this.add(k, v));
}
}