From d9683861f98277e7121ff47d2bd223c2d0c2cd34 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 1 Feb 2018 07:19:03 +0100 Subject: fix performance and UI issues with tipping --- src/query.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/query.ts') 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(store: Store, val: T, key: IDBValidKey): Promise { + 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(store: Store, val: T): Promise { 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); } -- cgit v1.2.3