summaryrefslogtreecommitdiff
path: root/packages
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2024-04-23 00:43:29 +0200
committerFlorian Dold <florian@dold.me>2024-04-23 00:43:29 +0200
commitf4ffd0fab12dadc709d6ced5d3129e0a2c03d72b (patch)
treeea4e32d56759c57dc4a42da0784a3d05421e3874 /packages
parent920a384e90bc35f262361920aa8ddd1046abac34 (diff)
downloadwallet-core-f4ffd0fab12dadc709d6ced5d3129e0a2c03d72b.tar.gz
wallet-core-f4ffd0fab12dadc709d6ced5d3129e0a2c03d72b.tar.bz2
wallet-core-f4ffd0fab12dadc709d6ced5d3129e0a2c03d72b.zip
wallet-core: prepare for cancellable DB access handle
Diffstat (limited to 'packages')
-rw-r--r--packages/taler-wallet-core/src/db.ts29
-rw-r--r--packages/taler-wallet-core/src/query.ts8
-rw-r--r--packages/taler-wallet-core/src/wallet.ts36
3 files changed, 56 insertions, 17 deletions
diff --git a/packages/taler-wallet-core/src/db.ts b/packages/taler-wallet-core/src/db.ts
index 5bab70968..5da8518ea 100644
--- a/packages/taler-wallet-core/src/db.ts
+++ b/packages/taler-wallet-core/src/db.ts
@@ -34,6 +34,7 @@ import {
Amounts,
AttentionInfo,
BackupProviderTerms,
+ CancellationToken,
Codec,
CoinEnvelope,
CoinPublicKeyString,
@@ -3269,7 +3270,12 @@ export async function openStoredBackupsDatabase(
onStoredBackupsDbUpgradeNeeded,
);
- const handle = new DbAccessImpl(backupsDbHandle, StoredBackupStores);
+ const handle = new DbAccessImpl(
+ backupsDbHandle,
+ StoredBackupStores,
+ {},
+ CancellationToken.CONTINUE,
+ );
return handle;
}
@@ -3283,7 +3289,7 @@ export async function openStoredBackupsDatabase(
export async function openTalerDatabase(
idbFactory: IDBFactory,
onVersionChange: () => void,
-): Promise<DbAccess<typeof WalletStoresV1>> {
+): Promise<IDBDatabase> {
const metaDbHandle = await openDatabase(
idbFactory,
TALER_WALLET_META_DB_NAME,
@@ -3292,7 +3298,12 @@ export async function openTalerDatabase(
onMetaDbUpgradeNeeded,
);
- const metaDb = new DbAccessImpl(metaDbHandle, walletMetadataStore);
+ const metaDb = new DbAccessImpl(
+ metaDbHandle,
+ walletMetadataStore,
+ {},
+ CancellationToken.CONTINUE,
+ );
let currentMainVersion: string | undefined;
await metaDb.runReadWriteTx(["metaConfig"], async (tx) => {
const dbVersionRecord = await tx.metaConfig.get(CURRENT_DB_CONFIG_KEY);
@@ -3341,11 +3352,15 @@ export async function openTalerDatabase(
onTalerDbUpgradeNeeded,
);
- const handle = new DbAccessImpl(mainDbHandle, WalletStoresV1);
-
- await applyFixups(handle);
+ const mainDbAccess = new DbAccessImpl(
+ mainDbHandle,
+ WalletStoresV1,
+ {},
+ CancellationToken.CONTINUE,
+ );
+ await applyFixups(mainDbAccess);
- return handle;
+ return mainDbHandle;
}
export async function deleteTalerDatabase(
diff --git a/packages/taler-wallet-core/src/query.ts b/packages/taler-wallet-core/src/query.ts
index d78e9bc6e..bdc8df0c7 100644
--- a/packages/taler-wallet-core/src/query.ts
+++ b/packages/taler-wallet-core/src/query.ts
@@ -35,7 +35,12 @@ import {
IDBValidKey,
IDBVersionChangeEvent,
} from "@gnu-taler/idb-bridge";
-import { Codec, Logger, openPromise } from "@gnu-taler/taler-util";
+import {
+ CancellationToken,
+ Codec,
+ Logger,
+ openPromise,
+} from "@gnu-taler/taler-util";
const logger = new Logger("query.ts");
@@ -814,6 +819,7 @@ export class DbAccessImpl<StoreMap> implements DbAccess<StoreMap> {
private db: IDBDatabase,
private stores: StoreMap,
private triggers: TriggerSpec = {},
+ private cancellationToken: CancellationToken,
) {}
idbHandle(): IDBDatabase {
diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts
index f743e82b1..9f9b90446 100644
--- a/packages/taler-wallet-core/src/wallet.ts
+++ b/packages/taler-wallet-core/src/wallet.ts
@@ -22,7 +22,7 @@
/**
* Imports.
*/
-import { IDBFactory } from "@gnu-taler/idb-bridge";
+import { IDBDatabase, IDBFactory } from "@gnu-taler/idb-bridge";
import {
AbsoluteTime,
ActiveTask,
@@ -243,7 +243,7 @@ import {
checkPeerPushDebit,
initiatePeerPushDebit,
} from "./pay-peer-push-debit.js";
-import { DbAccess } from "./query.js";
+import { DbAccess, DbAccessImpl } from "./query.js";
import { forceRefresh } from "./refresh.js";
import {
TaskScheduler,
@@ -1020,7 +1020,7 @@ async function dispatchRequestInternal<Op extends WalletApiOperation>(
const tasksInfo = await Promise.all(
allTasksId.map(async (id) => {
- return await wex.ws.db.runReadOnlyTx(
+ return await wex.db.runReadOnlyTx(
["operationRetries"],
async (tx) => {
return tx.operationRetries.get(id);
@@ -1758,15 +1758,19 @@ export class InternalWalletState {
private _config: Readonly<WalletRunConfig> | undefined;
- private _db: DbAccess<typeof WalletStoresV1> | undefined = undefined;
+ private _indexedDbHandle: IDBDatabase | undefined = undefined;
+
+ private _dbAccessHandle: DbAccess<typeof WalletStoresV1> | undefined;
private _http: HttpRequestLibrary | undefined = undefined;
get db(): DbAccess<typeof WalletStoresV1> {
- if (!this._db) {
- throw Error("db not initialized");
+ if (!this._dbAccessHandle) {
+ this._dbAccessHandle = this.createDbAccessHandle(
+ CancellationToken.CONTINUE,
+ );
}
- return this._db;
+ return this._dbAccessHandle;
}
devExperimentState: DevExperimentState = {};
@@ -1791,6 +1795,20 @@ export class InternalWalletState {
}
}
+ createDbAccessHandle(
+ cancellationToken: CancellationToken,
+ ): DbAccess<typeof WalletStoresV1> {
+ if (!this._indexedDbHandle) {
+ throw Error("db not initialized");
+ }
+ return new DbAccessImpl(
+ this._indexedDbHandle,
+ WalletStoresV1,
+ {},
+ cancellationToken,
+ );
+ }
+
get config(): WalletRunConfig {
if (!this._config) {
throw Error("config not initialized");
@@ -1817,7 +1835,7 @@ export class InternalWalletState {
}
async ensureWalletDbOpen(): Promise<void> {
- if (this._db) {
+ if (this._indexedDbHandle) {
return;
}
const myVersionChange = async (): Promise<void> => {
@@ -1825,7 +1843,7 @@ export class InternalWalletState {
};
try {
const myDb = await openTalerDatabase(this.idb, myVersionChange);
- this._db = myDb;
+ this._indexedDbHandle = myDb;
} catch (e) {
logger.error("error writing to database during initialization");
throw TalerError.fromDetail(TalerErrorCode.WALLET_DB_UNAVAILABLE, {