diff --git a/src/storage/idb/utils.js b/src/storage/idb/utils.js index 004692c2..c48453f2 100644 --- a/src/storage/idb/utils.js +++ b/src/storage/idb/utils.js @@ -1,11 +1,11 @@ export function openDatabase(name, createObjectStore, version = undefined) { - const req = window.indexedDB.open(name, version); - req.onupgradeneeded = (ev) => { - const db = ev.target.result; - const oldVersion = ev.oldVersion; - createObjectStore(db, oldVersion, version); - }; - return reqAsPromise(req); + const req = window.indexedDB.open(name, version); + req.onupgradeneeded = (ev) => { + const db = ev.target.result; + const oldVersion = ev.oldVersion; + createObjectStore(db, oldVersion, version); + }; + return reqAsPromise(req); } export function reqAsPromise(req) { @@ -23,7 +23,7 @@ export function txnAsPromise(txn) { } export function iterateCursor(cursor, processValue) { - // TODO: does cursor already have a value here?? + // TODO: does cursor already have a value here?? return new Promise((resolve, reject) => { cursor.onerror = (event) => { reject(new Error("Query failed: " + event.target.errorCode)); @@ -37,9 +37,9 @@ export function iterateCursor(cursor, processValue) { } const isDone = processValue(cursor.value); if (isDone) { - resolve(true); + resolve(true); } else { - cursor.continue(); + cursor.continue(); } }; }); @@ -48,56 +48,56 @@ export function iterateCursor(cursor, processValue) { export async function fetchResults(cursor, isDone) { const results = []; await iterateCursor(cursor, (value) => { - results.push(value); - return isDone(results); + results.push(value); + return isDone(results); }); return results; } export async function select(db, storeName, toCursor, isDone) { - if (!isDone) { - isDone = () => false; - } - if (!toCursor) { - toCursor = store => store.openCursor(); - } - const tx = db.transaction([storeName], "readonly"); - const store = tx.objectStore(storeName); - const cursor = toCursor(store); - return await fetchResults(cursor, isDone); + if (!isDone) { + isDone = () => false; + } + if (!toCursor) { + toCursor = store => store.openCursor(); + } + const tx = db.transaction([storeName], "readonly"); + const store = tx.objectStore(storeName); + const cursor = toCursor(store); + return await fetchResults(cursor, isDone); } export async function updateSingletonStore(db, storeName, value) { - const tx = db.transaction([storeName], "readwrite"); - const store = tx.objectStore(storeName); - const cursor = await reqAsPromise(store.openCursor()); - if (cursor) { - return reqAsPromise(cursor.update(storeName)); - } else { - return reqAsPromise(store.add(value)); - } + const tx = db.transaction([storeName], "readwrite"); + const store = tx.objectStore(storeName); + const cursor = await reqAsPromise(store.openCursor()); + if (cursor) { + return reqAsPromise(cursor.update(storeName)); + } else { + return reqAsPromise(store.add(value)); + } } export async function findStoreValue(db, storeName, toCursor, matchesValue) { - if (!matchesValue) { - matchesValue = () => true; - } - if (!toCursor) { - toCursor = store => store.openCursor(); - } + if (!matchesValue) { + matchesValue = () => true; + } + if (!toCursor) { + toCursor = store => store.openCursor(); + } - const tx = db.transaction([storeName], "readwrite"); - const store = tx.objectStore(storeName); - const cursor = await reqAsPromise(toCursor(store)); - let match; - const matched = await iterateCursor(cursor, (value) => { - if (matchesValue(value)) { - match = value; - return true; - } - }); - if (!matched) { - throw new Error("Value not found"); - } - return match; + const tx = db.transaction([storeName], "readwrite"); + const store = tx.objectStore(storeName); + const cursor = await reqAsPromise(toCursor(store)); + let match; + const matched = await iterateCursor(cursor, (value) => { + if (matchesValue(value)) { + match = value; + return true; + } + }); + if (!matched) { + throw new Error("Value not found"); + } + return match; } \ No newline at end of file