From 16a5bb40834c01e50e84144bb644517e67a66187 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 20 Sep 2022 21:44:21 +0200 Subject: wallet-core: make basic backup work again --- packages/taler-wallet-core/src/util/query.ts | 65 ++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 17 deletions(-) (limited to 'packages/taler-wallet-core/src/util') diff --git a/packages/taler-wallet-core/src/util/query.ts b/packages/taler-wallet-core/src/util/query.ts index 8b8c30f35..d1aae6fd6 100644 --- a/packages/taler-wallet-core/src/util/query.ts +++ b/packages/taler-wallet-core/src/util/query.ts @@ -409,10 +409,12 @@ export type GetReadWriteAccess = { type ReadOnlyTransactionFunction = ( t: GetReadOnlyAccess, + rawTx: IDBTransaction, ) => Promise; type ReadWriteTransactionFunction = ( t: GetReadWriteAccess, + rawTx: IDBTransaction, ) => Promise; export interface TransactionContext { @@ -420,22 +422,10 @@ export interface TransactionContext { runReadOnly(f: ReadOnlyTransactionFunction): Promise; } -type CheckDescriptor = T extends StoreWithIndexes< - infer SN, - infer SD, - infer IM -> - ? StoreWithIndexes - : unknown; - -type GetPickerType = F extends (x: SM) => infer Out - ? { [P in keyof Out]: CheckDescriptor } - : unknown; - function runTx( tx: IDBTransaction, arg: Arg, - f: (t: Arg) => Promise, + f: (t: Arg, t2: IDBTransaction) => Promise, ): Promise { const stack = Error("Failed transaction was started here."); return new Promise((resolve, reject) => { @@ -474,7 +464,7 @@ function runTx( logger.error(msg); reject(new TransactionAbortedError(msg)); }; - const resP = Promise.resolve().then(() => f(arg)); + const resP = Promise.resolve().then(() => f(arg, tx)); resP .then((result) => { gotFunResult = true; @@ -624,6 +614,46 @@ export class DbAccess { return this.db; } + /** + * Run a transaction with all object stores. + */ + mktxAll(): TransactionContext { + const storeNames: string[] = []; + const accessibleStores: { [x: string]: StoreWithIndexes } = + {}; + + for (let i = 0; i < this.db.objectStoreNames.length; i++) { + const sn = this.db.objectStoreNames[i]; + const swi = (this.stores as any)[sn] as StoreWithIndexes; + if (!swi) { + throw Error(`store metadata not available (${sn})`); + } + storeNames.push(sn); + accessibleStores[sn] = swi; + } + + const runReadOnly = ( + txf: ReadOnlyTransactionFunction, + ): Promise => { + const tx = this.db.transaction(storeNames, "readonly"); + const readContext = makeReadContext(tx, accessibleStores); + return runTx(tx, readContext, txf); + }; + + const runReadWrite = ( + txf: ReadWriteTransactionFunction, + ): Promise => { + const tx = this.db.transaction(storeNames, "readwrite"); + const writeContext = makeWriteContext(tx, accessibleStores); + return runTx(tx, writeContext, txf); + }; + + return { + runReadOnly, + runReadWrite, + }; + } + /** * Run a transaction with selected object stores. * @@ -638,13 +668,14 @@ export class DbAccess { [X in StoreNamesOf]: StoreList[number] & { storeName: X }; }, >(namePicker: (x: StoreMap) => StoreList): TransactionContext { + const storeNames: string[] = []; + const accessibleStores: { [x: string]: StoreWithIndexes } = + {}; + const storePick = namePicker(this.stores) as any; if (typeof storePick !== "object" || storePick === null) { throw Error(); } - const storeNames: string[] = []; - const accessibleStores: { [x: string]: StoreWithIndexes } = - {}; for (const swiPicked of storePick) { const swi = swiPicked as StoreWithIndexes; if (swi.mark !== storeWithIndexesSymbol) { -- cgit v1.2.3