diff options
Diffstat (limited to 'packages/idb-bridge/src')
-rw-r--r-- | packages/idb-bridge/src/backends.test.ts | 51 | ||||
-rw-r--r-- | packages/idb-bridge/src/bridge-idb.ts | 12 |
2 files changed, 55 insertions, 8 deletions
diff --git a/packages/idb-bridge/src/backends.test.ts b/packages/idb-bridge/src/backends.test.ts index 684358eac..64133a73b 100644 --- a/packages/idb-bridge/src/backends.test.ts +++ b/packages/idb-bridge/src/backends.test.ts @@ -23,24 +23,23 @@ * Imports. */ import test from "ava"; +import { MemoryBackend } from "./MemoryBackend.js"; import { BridgeIDBCursorWithValue, BridgeIDBDatabase, BridgeIDBFactory, BridgeIDBKeyRange, - BridgeIDBTransaction, } from "./bridge-idb.js"; +import { promiseFromRequest, promiseFromTransaction } from "./idbpromutil.js"; import { IDBCursorDirection, IDBCursorWithValue, IDBDatabase, IDBKeyRange, - IDBRequest, IDBValidKey, } from "./idbtypes.js"; import { initTestIndexedDB, useTestIndexedDb } from "./testingdb.js"; -import { MemoryBackend } from "./MemoryBackend.js"; -import { promiseFromRequest, promiseFromTransaction } from "./idbpromutil.js"; +import { promiseForTransaction } from "./idb-wpt-ported/wptsupport.js"; test.before("test DB initialization", initTestIndexedDB); @@ -464,8 +463,7 @@ test("export", async (t) => { const exportedData2 = backend2.exportDump(); t.assert( - exportedData.databases[dbname].objectStores["books"].records.length === - 3, + exportedData.databases[dbname].objectStores["books"].records.length === 3, ); t.deepEqual(exportedData, exportedData2); @@ -738,3 +736,44 @@ test("range queries", async (t) => { t.pass(); }); + +test("idb: multiple transactions", async (t) => { + const idb = useTestIndexedDb(); + const dbname = "mtx-" + new Date().getTime() + Math.random(); + + const request = idb.open(dbname); + request.onupgradeneeded = () => { + const db = request.result as BridgeIDBDatabase; + const store = db.createObjectStore("books", { keyPath: "isbn" }); + const titleIndex = store.createIndex("by_title", "title", { unique: true }); + const authorIndex = store.createIndex("by_author", "author"); + + // Populate with initial data. + store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 }); + store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 }); + store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 }); + }; + + const db: BridgeIDBDatabase = await promiseFromRequest(request); + + const tx1 = db.transaction(["books"], "readwrite"); + + tx1.oncomplete = () => { + t.log("first tx completed"); + }; + + tx1.onerror = () => { + t.log("first tx errored"); + }; + + tx1.abort(); + + const tx2 = db.transaction(["books"], "readwrite"); + tx2.commit(); + + const tx3 = db.transaction(["books"], "readwrite"); + + await promiseForTransaction(t, tx3); + + t.pass(); +}); diff --git a/packages/idb-bridge/src/bridge-idb.ts b/packages/idb-bridge/src/bridge-idb.ts index afb3f4224..15c68c733 100644 --- a/packages/idb-bridge/src/bridge-idb.ts +++ b/packages/idb-bridge/src/bridge-idb.ts @@ -2730,16 +2730,24 @@ export class BridgeIDBTransaction if (BridgeIDBFactory.enableTracing) { console.log("beginning backend transaction to process operation"); } - this._backendTransaction = await this._backend.beginTransaction( + const newBackendTx = await this._backend.beginTransaction( this._db._backendConnection, Array.from(this._scope), this.mode, ); if (BridgeIDBFactory.enableTracing) { console.log( - `started backend transaction (${this._backendTransaction.transactionCookie})`, + `started backend transaction (${newBackendTx.transactionCookie})`, ); } + if (this._aborted) { + // Probably there is a more elegant way to do this by aborting the + // beginTransaction call when the transaction was aborted. + // That would require changing the DB backend API. + this._backend.rollback(newBackendTx); + } else { + this._backendTransaction = newBackendTx; + } } if (!request._source) { |