diff options
author | Florian Dold <florian@dold.me> | 2021-02-22 14:27:54 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-02-22 14:27:54 +0100 |
commit | 3eced74a88de43ab9afe542fcce20a8db8e3fe60 (patch) | |
tree | 8ef74cfd5cf3bea8fe90cd20746e4fadb0afc349 /packages/idb-bridge/src/bridge-idb.ts | |
parent | e6946694f2e7ae6ff25f490fa76f3da583c44c74 (diff) | |
download | wallet-core-3eced74a88de43ab9afe542fcce20a8db8e3fe60.tar.xz |
more tests, fix event ordering issue
Diffstat (limited to 'packages/idb-bridge/src/bridge-idb.ts')
-rw-r--r-- | packages/idb-bridge/src/bridge-idb.ts | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/packages/idb-bridge/src/bridge-idb.ts b/packages/idb-bridge/src/bridge-idb.ts index 643a98dea..ceba618db 100644 --- a/packages/idb-bridge/src/bridge-idb.ts +++ b/packages/idb-bridge/src/bridge-idb.ts @@ -1609,6 +1609,10 @@ export class BridgeIDBObjectStore implements IDBObjectStore { throw new TypeError(); } + if (!this._transaction._active) { + throw new TransactionInactiveError(); + } + if (this._deleted) { throw new InvalidStateError( "tried to call 'delete' on a deleted object store", @@ -1918,6 +1922,8 @@ export class BridgeIDBRequest extends FakeEventTarget implements IDBRequest { onsuccess: EventListener | null = null; onerror: EventListener | null = null; + _debugName: string | undefined; + get error() { if (this.readyState === "pending") { throw new InvalidStateError(); @@ -1998,6 +2004,25 @@ export class BridgeIDBOpenDBRequest } } +function waitMacroQueue(): Promise<void> { + return new Promise<void>((resolve, reject) => { + let immCalled = false; + let timeoutCalled = false; + setImmediate(() => { + immCalled = true; + if (immCalled && timeoutCalled) { + resolve(); + } + }); + setTimeout(() => { + timeoutCalled = true; + if (immCalled && timeoutCalled) { + resolve(); + } + }, 0); + }); +} + // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#transaction /** @public */ export class BridgeIDBTransaction @@ -2182,7 +2207,7 @@ export class BridgeIDBTransaction // http://w3c.github.io/IndexedDB/#dom-idbtransaction-objectstore public objectStore(name: string): BridgeIDBObjectStore { if (!this._active) { - throw new InvalidStateError(); + throw new TransactionInactiveError(); } if (!this._db._schema.objectStores[name]) { @@ -2279,6 +2304,8 @@ export class BridgeIDBTransaction } } + await waitMacroQueue(); + if (!request._source) { // Special requests like indexes that just need to run some code, // with error handling already built into operation @@ -2289,9 +2316,12 @@ export class BridgeIDBTransaction BridgeIDBFactory.enableTracing && console.log("TRACE: running operation in transaction"); const result = await operation(); + // Wait until setTimeout/setImmediate tasks are run BridgeIDBFactory.enableTracing && console.log( - "TRACE: operation in transaction finished with success", + `TRACE: request (${ + request._debugName ?? "??" + }) in transaction finished with success`, ); request.readyState = "done"; request.result = result; @@ -2304,6 +2334,10 @@ export class BridgeIDBTransaction cancelable: false, }); + queueTask(() => { + this._active = false; + }); + try { event.eventPath = [this._db, this]; request.dispatchEvent(event); @@ -2372,7 +2406,11 @@ export class BridgeIDBTransaction this._committed = true; if (!this._error) { if (BridgeIDBFactory.enableTracing) { - console.log("dispatching 'complete' event on transaction"); + console.log( + `dispatching 'complete' event on transaction (${ + this._debugName ?? "??" + })`, + ); } const event = new FakeEvent("complete"); event.eventPath = [this._db, this]; |