This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/matrix/storage/idb/utils.js

125 lines
3.7 KiB
JavaScript
Raw Normal View History

import { StorageError } from "../common.js";
2019-07-01 13:30:29 +05:30
// storage keys are defined to be unsigned 32bit numbers in WebPlatform.js, which is assumed by idb
export function encodeUint32(n) {
const hex = n.toString(16);
return "0".repeat(8 - hex.length) + hex;
}
export function decodeUint32(str) {
return parseInt(str, 16);
}
2019-02-07 04:49:14 +05:30
export function openDatabase(name, createObjectStore, version) {
2019-02-07 03:36:56 +05:30
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);
}
function wrapError(err) {
return new StorageError(`wrapped DOMException`, err);
}
export function reqAsPromise(req) {
return new Promise((resolve, reject) => {
2019-02-07 03:36:33 +05:30
req.addEventListener("success", event => resolve(event.target.result));
req.addEventListener("error", event => reject(wrapError(event.target.error)));
});
}
export function txnAsPromise(txn) {
return new Promise((resolve, reject) => {
txn.addEventListener("complete", resolve);
txn.addEventListener("abort", e => reject(wrapError(e)));
});
}
export function iterateCursor(cursorRequest, processValue) {
2019-02-07 03:36:56 +05:30
// TODO: does cursor already have a value here??
return new Promise((resolve, reject) => {
cursorRequest.onerror = () => {
reject(new StorageError("Query failed", cursorRequest.error));
};
// collect results
cursorRequest.onsuccess = (event) => {
const cursor = event.target.result;
if (!cursor) {
resolve(false);
return; // end of results
}
const {done, jumpTo} = processValue(cursor.value, cursor.key);
if (done) {
2019-02-07 03:36:56 +05:30
resolve(true);
} else if(jumpTo) {
cursor.continue(jumpTo);
} else {
cursor.continue();
}
};
}).catch(err => {
throw new StorageError("iterateCursor failed", err);
});
}
export async function fetchResults(cursor, isDone) {
const results = [];
await iterateCursor(cursor, (value) => {
2019-02-07 03:36:56 +05:30
results.push(value);
return {done: isDone(results)};
});
return results;
}
export async function select(db, storeName, toCursor, isDone) {
2019-02-07 03:36:56 +05:30
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) {
2019-02-07 03:36:56 +05:30
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) {
2019-02-07 03:36:56 +05:30
if (!matchesValue) {
matchesValue = () => true;
}
if (!toCursor) {
toCursor = store => store.openCursor();
}
2019-02-07 03:36:56 +05:30
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 StorageError("Value not found");
2019-02-07 03:36:56 +05:30
}
return match;
}