-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathdb-chrome-storage.js
70 lines (62 loc) · 1.85 KB
/
db-chrome-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
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
import {DB, UCD} from '@/js/consts';
import {chromeLocal, GET_KEYS} from '@/js/storage-util';
export default class ChromeStorageDB {
constructor(dbName, mirror) {
this._max = dbName === DB ? 0 : 1;
this._mirror = mirror;
this._prefix = dbName === DB ? 'style-' : `${dbName}-`;
}
delete(id) {
return chromeLocal.remove(this._prefix + id);
}
async get(id) {
return (await chromeLocal.get(id = this._prefix + id))[id];
}
async getAll() {
const all = !GET_KEYS && await chromeLocal.get();
const keys = GET_KEYS ? await chromeLocal.getKeys() : Object.keys(all);
const res = [];
if (!this._max)
await this._init(keys);
for (const key of keys)
if (key.startsWith(this._prefix))
res.push(GET_KEYS ? key : all[key]);
return GET_KEYS
? Object.values(await chromeLocal.get(res))
: res;
}
async put(item, key) {
key ??= item.id ??= (!this._max && await this._init(), this._max++);
await chromeLocal.set({
[this._prefix + key]: this._mirror && item[UCD]
? {...item, sections: undefined}
: item,
});
return key;
}
async putMany(items, keys) {
const data = {};
const res = [];
for (let i = 0; i < items.length; i++) {
const item = items[i];
const id = keys ? keys[i] : item.id ??= (!this._max && await this._init(), this._max++);
data[this._prefix + id] = this._mirror && item[UCD]
? {...item, sections: undefined}
: item;
res.push(id);
}
await chromeLocal.set(data);
return res;
}
async _init(keys) {
let res = 1;
let id;
keys ??= GET_KEYS
? await chromeLocal.getKeys()
: Object.keys(await chromeLocal.get());
for (const key of keys)
if (key.startsWith(this._prefix) && (id = +key.slice(this._prefix.length)) >= res)
res = id + 1;
this._max = res;
}
}