refactor(es modules): modulize utils

This commit is contained in:
Azgaar 2022-06-26 19:20:31 +03:00
parent 11df349394
commit 12e1c9f334
45 changed files with 620 additions and 283 deletions

50
src/scripts/indexedDB.js Normal file
View file

@ -0,0 +1,50 @@
// indexedDB support: ldb object
// @ts-ignore unimplemented historical interfaces
const indexedDBfactory = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
if (!indexedDBfactory) console.error("indexedDB not supported");
let database;
const databaseRequest = indexedDBfactory.open("d2", 1);
databaseRequest.onsuccess = function () {
database = this.result;
};
databaseRequest.onerror = function (e) {
console.error("indexedDB request error", e);
};
databaseRequest.onupgradeneeded = function (event) {
database = null;
const store = databaseRequest.result.createObjectStore("s", {keyPath: "k"});
store.transaction.oncomplete = function (e) {
database = e.target.db;
};
};
function getValue(key, callback) {
if (!database) {
setTimeout(() => getValue(key, callback), 100);
return;
}
database.transaction("s").objectStore("s").get(key).onsuccess = function (e) {
const value = (e.target.result && e.target.result.v) || null;
callback(value);
};
}
function setValue(key) {
if (!database) {
setTimeout(() => setValue(key, value), 100);
return;
}
database
.transaction("s", "readwrite")
.objectStore("s")
.put({[key]: value});
}
export const ldb = {get: getValue, set: setValue};