mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 11:35:04 +01:00
whitespace
This commit is contained in:
parent
511e91a699
commit
27be261a5d
@ -1,11 +1,11 @@
|
|||||||
export function openDatabase(name, createObjectStore, version = undefined) {
|
export function openDatabase(name, createObjectStore, version = undefined) {
|
||||||
const req = window.indexedDB.open(name, version);
|
const req = window.indexedDB.open(name, version);
|
||||||
req.onupgradeneeded = (ev) => {
|
req.onupgradeneeded = (ev) => {
|
||||||
const db = ev.target.result;
|
const db = ev.target.result;
|
||||||
const oldVersion = ev.oldVersion;
|
const oldVersion = ev.oldVersion;
|
||||||
createObjectStore(db, oldVersion, version);
|
createObjectStore(db, oldVersion, version);
|
||||||
};
|
};
|
||||||
return reqAsPromise(req);
|
return reqAsPromise(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function reqAsPromise(req) {
|
export function reqAsPromise(req) {
|
||||||
@ -23,7 +23,7 @@ export function txnAsPromise(txn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function iterateCursor(cursor, processValue) {
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
cursor.onerror = (event) => {
|
cursor.onerror = (event) => {
|
||||||
reject(new Error("Query failed: " + event.target.errorCode));
|
reject(new Error("Query failed: " + event.target.errorCode));
|
||||||
@ -37,9 +37,9 @@ export function iterateCursor(cursor, processValue) {
|
|||||||
}
|
}
|
||||||
const isDone = processValue(cursor.value);
|
const isDone = processValue(cursor.value);
|
||||||
if (isDone) {
|
if (isDone) {
|
||||||
resolve(true);
|
resolve(true);
|
||||||
} else {
|
} else {
|
||||||
cursor.continue();
|
cursor.continue();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@ -48,56 +48,56 @@ export function iterateCursor(cursor, processValue) {
|
|||||||
export async function fetchResults(cursor, isDone) {
|
export async function fetchResults(cursor, isDone) {
|
||||||
const results = [];
|
const results = [];
|
||||||
await iterateCursor(cursor, (value) => {
|
await iterateCursor(cursor, (value) => {
|
||||||
results.push(value);
|
results.push(value);
|
||||||
return isDone(results);
|
return isDone(results);
|
||||||
});
|
});
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function select(db, storeName, toCursor, isDone) {
|
export async function select(db, storeName, toCursor, isDone) {
|
||||||
if (!isDone) {
|
if (!isDone) {
|
||||||
isDone = () => false;
|
isDone = () => false;
|
||||||
}
|
}
|
||||||
if (!toCursor) {
|
if (!toCursor) {
|
||||||
toCursor = store => store.openCursor();
|
toCursor = store => store.openCursor();
|
||||||
}
|
}
|
||||||
const tx = db.transaction([storeName], "readonly");
|
const tx = db.transaction([storeName], "readonly");
|
||||||
const store = tx.objectStore(storeName);
|
const store = tx.objectStore(storeName);
|
||||||
const cursor = toCursor(store);
|
const cursor = toCursor(store);
|
||||||
return await fetchResults(cursor, isDone);
|
return await fetchResults(cursor, isDone);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateSingletonStore(db, storeName, value) {
|
export async function updateSingletonStore(db, storeName, value) {
|
||||||
const tx = db.transaction([storeName], "readwrite");
|
const tx = db.transaction([storeName], "readwrite");
|
||||||
const store = tx.objectStore(storeName);
|
const store = tx.objectStore(storeName);
|
||||||
const cursor = await reqAsPromise(store.openCursor());
|
const cursor = await reqAsPromise(store.openCursor());
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
return reqAsPromise(cursor.update(storeName));
|
return reqAsPromise(cursor.update(storeName));
|
||||||
} else {
|
} else {
|
||||||
return reqAsPromise(store.add(value));
|
return reqAsPromise(store.add(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function findStoreValue(db, storeName, toCursor, matchesValue) {
|
export async function findStoreValue(db, storeName, toCursor, matchesValue) {
|
||||||
if (!matchesValue) {
|
if (!matchesValue) {
|
||||||
matchesValue = () => true;
|
matchesValue = () => true;
|
||||||
}
|
}
|
||||||
if (!toCursor) {
|
if (!toCursor) {
|
||||||
toCursor = store => store.openCursor();
|
toCursor = store => store.openCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
const tx = db.transaction([storeName], "readwrite");
|
const tx = db.transaction([storeName], "readwrite");
|
||||||
const store = tx.objectStore(storeName);
|
const store = tx.objectStore(storeName);
|
||||||
const cursor = await reqAsPromise(toCursor(store));
|
const cursor = await reqAsPromise(toCursor(store));
|
||||||
let match;
|
let match;
|
||||||
const matched = await iterateCursor(cursor, (value) => {
|
const matched = await iterateCursor(cursor, (value) => {
|
||||||
if (matchesValue(value)) {
|
if (matchesValue(value)) {
|
||||||
match = value;
|
match = value;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (!matched) {
|
if (!matched) {
|
||||||
throw new Error("Value not found");
|
throw new Error("Value not found");
|
||||||
}
|
}
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user