aboutsummaryrefslogtreecommitdiff
path: root/src/query.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-02-01 07:19:03 +0100
committerFlorian Dold <florian.dold@gmail.com>2018-02-01 07:19:03 +0100
commitd9683861f98277e7121ff47d2bd223c2d0c2cd34 (patch)
treea7357a7ebe80675716b543f1d30f1f1cdf083efa /src/query.ts
parent97f6e68ce3a515938228b9a4d3e41b5f4b25a015 (diff)
downloadwallet-core-d9683861f98277e7121ff47d2bd223c2d0c2cd34.tar.xz
fix performance and UI issues with tipping
Diffstat (limited to 'src/query.ts')
-rw-r--r--src/query.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/query.ts b/src/query.ts
index e45596c66..f21f82020 100644
--- a/src/query.ts
+++ b/src/query.ts
@@ -697,6 +697,31 @@ export class QueryRoot {
return this;
}
+ /**
+ * Put an object into a store or return an existing record.
+ */
+ putOrGetExisting<T>(store: Store<T>, val: T, key: IDBValidKey): Promise<T> {
+ this.checkFinished();
+ const {resolve, promise} = openPromise();
+ const doPutOrGet = (tx: IDBTransaction) => {
+ const objstore = tx.objectStore(store.name);
+ const req = objstore.get(key);
+ req.onsuccess = () => {
+ if (req.result !== undefined) {
+ resolve(req.result);
+ } else {
+ const req2 = objstore.add(val);
+ req2.onsuccess = () => {
+ resolve(val);
+ };
+ }
+ };
+ };
+ this.scheduleFinish();
+ this.addWork(doPutOrGet, store.name, true);
+ return promise;
+ }
+
putWithResult<T>(store: Store<T>, val: T): Promise<IDBValidKey> {
this.checkFinished();
@@ -892,8 +917,12 @@ export class QueryRoot {
resolve();
};
tx.onabort = () => {
+ console.warn(`aborted ${mode} transaction on stores [${[... this.stores]}]`);
reject(Error("transaction aborted"));
};
+ tx.onerror = (e) => {
+ console.warn(`error in transaction`, (e.target as any).error);
+ };
for (const w of this.work) {
w(tx);
}