2021-01-01 14:27:58 +00:00
|
|
|
/* global chromeLocal */// storage-util.js
|
2020-02-10 14:56:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/* exported createChromeStorageDB */
|
2022-01-23 09:44:25 +00:00
|
|
|
function createChromeStorageDB(PREFIX) {
|
2020-02-10 14:56:07 +00:00
|
|
|
let INC;
|
|
|
|
|
2022-01-23 09:44:25 +00:00
|
|
|
return {
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
delete(id) {
|
|
|
|
return chromeLocal.remove(PREFIX + id);
|
|
|
|
},
|
|
|
|
|
|
|
|
get(id) {
|
|
|
|
return chromeLocal.getValue(PREFIX + id);
|
|
|
|
},
|
2020-02-10 14:56:07 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async getAll() {
|
|
|
|
const all = await chromeLocal.get();
|
|
|
|
if (!INC) prepareInc(all);
|
|
|
|
return Object.entries(all)
|
|
|
|
.map(([key, val]) => key.startsWith(PREFIX) && Number(key.slice(PREFIX.length)) && val)
|
|
|
|
.filter(Boolean);
|
|
|
|
},
|
2020-02-10 14:56:07 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async put(item) {
|
|
|
|
if (!item.id) {
|
|
|
|
if (!INC) await prepareInc();
|
|
|
|
item.id = INC++;
|
|
|
|
}
|
|
|
|
await chromeLocal.setValue(PREFIX + item.id, item);
|
|
|
|
return item.id;
|
|
|
|
},
|
|
|
|
|
|
|
|
async putMany(items) {
|
|
|
|
const data = {};
|
|
|
|
for (const item of items) {
|
|
|
|
if (!item.id) {
|
|
|
|
if (!INC) await prepareInc();
|
|
|
|
item.id = INC++;
|
|
|
|
}
|
|
|
|
data[PREFIX + item.id] = item;
|
|
|
|
}
|
|
|
|
await chromeLocal.set(data);
|
|
|
|
return items.map(_ => _.id);
|
|
|
|
},
|
|
|
|
};
|
2020-02-10 14:56:07 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function prepareInc(data) {
|
|
|
|
INC = 1;
|
|
|
|
for (const key in data || await chromeLocal.get()) {
|
|
|
|
if (key.startsWith(PREFIX)) {
|
|
|
|
const id = Number(key.slice(PREFIX.length));
|
|
|
|
if (id >= INC) {
|
|
|
|
INC = id + 1;
|
2020-02-10 14:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2020-02-10 14:56:07 +00:00
|
|
|
}
|
|
|
|
}
|