From 76ef6796888ddba2f29a3c634a36e5db72710345 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 24 Mar 2017 16:49:45 +0100 Subject: fix transaction issue and add safeguards --- src/query.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/query.ts b/src/query.ts index 89d4133b3..810088169 100644 --- a/src/query.ts +++ b/src/query.ts @@ -106,9 +106,9 @@ abstract class BaseQueryValue implements QueryValue { return new Promise((resolve, reject) => { this.subscribeOne((v, tx) => { if (f(v)) { - onTrue(this.root); + onTrue(new QueryRoot(this.root.db)); } else { - onFalse(this.root); + onFalse(new QueryRoot(this.root.db)); } }); resolve(); @@ -504,7 +504,7 @@ class IterQueryStream extends QueryStreamBase { export class QueryRoot implements PromiseLike { private work: ((t: IDBTransaction) => void)[] = []; - private db: IDBDatabase; + db: IDBDatabase; private stores = new Set(); private kickoffPromise: Promise; @@ -516,6 +516,8 @@ export class QueryRoot implements PromiseLike { private finishScheduled: boolean; + private finished: boolean = false; + constructor(db: IDBDatabase) { this.db = db; } @@ -524,13 +526,21 @@ export class QueryRoot implements PromiseLike { return this.finish().then(onfulfilled, onrejected); } + checkFinished() { + if (this.finished) { + throw Error("Can't add work to query after it was started"); + } + } + iter(store: Store): QueryStream { + this.checkFinished(); this.stores.add(store.name); this.scheduleFinish(); return new IterQueryStream(this, store.name, {}); } count(store: Store): Promise { + this.checkFinished(); const {resolve, promise} = openPromise(); const doCount = (tx: IDBTransaction) => { @@ -549,6 +559,7 @@ export class QueryRoot implements PromiseLike { } deleteIf(store: Store, predicate: (x: T, n: number) => boolean): QueryRoot { + this.checkFinished(); const doDeleteIf = (tx: IDBTransaction) => { const s = tx.objectStore(store.name); const req = s.openCursor(); @@ -569,6 +580,7 @@ export class QueryRoot implements PromiseLike { iterIndex(index: Index, only?: S): QueryStream { + this.checkFinished(); this.stores.add(index.storeName); this.scheduleFinish(); return new IterQueryStream(this, index.storeName, { @@ -583,6 +595,7 @@ export class QueryRoot implements PromiseLike { * in the store. */ put(store: Store, val: T): QueryRoot { + this.checkFinished(); let doPut = (tx: IDBTransaction) => { tx.objectStore(store.name).put(val); }; @@ -593,6 +606,7 @@ export class QueryRoot implements PromiseLike { putWithResult(store: Store, val: T): Promise { + this.checkFinished(); const {resolve, promise} = openPromise(); let doPutWithResult = (tx: IDBTransaction) => { let req = tx.objectStore(store.name).put(val); @@ -609,6 +623,7 @@ export class QueryRoot implements PromiseLike { mutate(store: Store, key: any, f: (v: T) => T): QueryRoot { + this.checkFinished(); let doPut = (tx: IDBTransaction) => { let reqGet = tx.objectStore(store.name).get(key); reqGet.onsuccess = () => { @@ -639,6 +654,7 @@ export class QueryRoot implements PromiseLike { * in the object store. */ putAll(store: Store, iterable: T[]): QueryRoot { + this.checkFinished(); const doPutAll = (tx: IDBTransaction) => { for (let obj of iterable) { tx.objectStore(store.name).put(obj); @@ -655,6 +671,7 @@ export class QueryRoot implements PromiseLike { * in the object store. */ add(store: Store, val: T): QueryRoot { + this.checkFinished(); const doAdd = (tx: IDBTransaction) => { tx.objectStore(store.name).add(val); }; @@ -667,6 +684,7 @@ export class QueryRoot implements PromiseLike { * Get one object from a store by its key. */ get(store: Store, key: any): Promise { + this.checkFinished(); if (key === void 0) { throw Error("key must not be undefined"); } @@ -691,6 +709,7 @@ export class QueryRoot implements PromiseLike { */ getIndexed(index: Index, key: I): Promise { + this.checkFinished(); if (key === void 0) { throw Error("key must not be undefined"); } @@ -727,6 +746,8 @@ export class QueryRoot implements PromiseLike { return this.kickoffPromise; } this.kickoffPromise = new Promise((resolve, reject) => { + // At this point, we can't add any more work + this.finished = true; if (this.work.length == 0) { resolve(); return; @@ -750,6 +771,7 @@ export class QueryRoot implements PromiseLike { * Delete an object by from the given object store. */ delete(storeName: string, key: any): QueryRoot { + this.checkFinished(); const doDelete = (tx: IDBTransaction) => { tx.objectStore(storeName).delete(key); }; -- cgit v1.2.3