summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2024-04-10 14:50:53 +0200
committerFlorian Dold <florian@dold.me>2024-04-10 14:50:53 +0200
commit4725d98ed550b8d38420438d307ce5dbf97e147d (patch)
treeceb35e243d017d5c99a5d4723726dd8e01c80382
parent234cebd7404c9c31f4d660aad9391231b37dd04a (diff)
downloadwallet-core-4725d98ed550b8d38420438d307ce5dbf97e147d.tar.gz
wallet-core-4725d98ed550b8d38420438d307ce5dbf97e147d.tar.bz2
wallet-core-4725d98ed550b8d38420438d307ce5dbf97e147d.zip
wallet-core: primitive DB trigger support
-rw-r--r--packages/taler-wallet-core/src/query.ts35
1 files changed, 31 insertions, 4 deletions
diff --git a/packages/taler-wallet-core/src/query.ts b/packages/taler-wallet-core/src/query.ts
index cffad84df..d78e9bc6e 100644
--- a/packages/taler-wallet-core/src/query.ts
+++ b/packages/taler-wallet-core/src/query.ts
@@ -31,6 +31,7 @@ import {
IDBKeyRange,
IDBRequest,
IDBTransaction,
+ IDBTransactionMode,
IDBValidKey,
IDBVersionChangeEvent,
} from "@gnu-taler/idb-bridge";
@@ -766,6 +767,7 @@ function makeWriteContext(
export interface DbAccess<StoreMap> {
idbHandle(): IDBDatabase;
+
runAllStoresReadWriteTx<T>(
options: {
label?: string;
@@ -774,6 +776,7 @@ export interface DbAccess<StoreMap> {
tx: DbReadWriteTransaction<StoreMap, Array<StoreNames<StoreMap>>>,
) => Promise<T>,
): Promise<T>;
+
runAllStoresReadOnlyTx<T>(
options: {
label?: string;
@@ -782,16 +785,25 @@ export interface DbAccess<StoreMap> {
tx: DbReadOnlyTransaction<StoreMap, Array<StoreNames<StoreMap>>>,
) => Promise<T>,
): Promise<T>;
+
runReadWriteTx<T, StoreNameArray extends Array<StoreNames<StoreMap>>>(
storeNames: StoreNameArray,
txf: (tx: DbReadWriteTransaction<StoreMap, StoreNameArray>) => Promise<T>,
): Promise<T>;
+
runReadOnlyTx<T, StoreNameArray extends Array<StoreNames<StoreMap>>>(
storeNames: StoreNameArray,
txf: (tx: DbReadOnlyTransaction<StoreMap, StoreNameArray>) => Promise<T>,
): Promise<T>;
}
+export interface TriggerSpec {
+ /**
+ * Trigger run after every successful commit, run outside of the transaction.
+ */
+ afterCommit?: (mode: IDBTransactionMode, stores: string[]) => void;
+}
+
/**
* Type-safe access to a database with a particular store map.
*
@@ -801,6 +813,7 @@ export class DbAccessImpl<StoreMap> implements DbAccess<StoreMap> {
constructor(
private db: IDBDatabase,
private stores: StoreMap,
+ private triggers: TriggerSpec = {},
) {}
idbHandle(): IDBDatabase {
@@ -861,9 +874,14 @@ export class DbAccessImpl<StoreMap> implements DbAccess<StoreMap> {
strStoreNames.push(swi.storeName);
accessibleStores[swi.storeName] = swi;
}
- const tx = this.db.transaction(strStoreNames, "readwrite");
+ const mode = "readwrite";
+ const tx = this.db.transaction(strStoreNames, mode);
const writeContext = makeWriteContext(tx, accessibleStores);
- return runTx(tx, writeContext, txf);
+ const res = runTx(tx, writeContext, txf);
+ if (this.triggers.afterCommit) {
+ this.triggers.afterCommit(mode, strStoreNames);
+ }
+ return res;
}
runReadOnlyTx<T, StoreNameArray extends Array<StoreNames<StoreMap>>>(
@@ -878,8 +896,17 @@ export class DbAccessImpl<StoreMap> implements DbAccess<StoreMap> {
strStoreNames.push(swi.storeName);
accessibleStores[swi.storeName] = swi;
}
- const tx = this.db.transaction(strStoreNames, "readonly");
+ const mode = "readonly";
+ const tx = this.db.transaction(strStoreNames, mode);
const readContext = makeReadContext(tx, accessibleStores);
- return runTx(tx, readContext, txf);
+ const res = runTx(tx, readContext, txf);
+ if (this.triggers.afterCommit) {
+ this.triggers.afterCommit(mode, strStoreNames);
+ }
+ return res;
}
+
+ registerPostCommitTrigger(args: {
+ handler: (storeNames: string[]) => void;
+ }): void {}
}