taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 195b6859cf9e2f5f3d47a03c31f8448d3b55a051
parent 4887e9f19e7e1c4ad988c865980b6de3bff39c23
Author: Florian Dold <florian@dold.me>
Date:   Tue, 24 Sep 2024 16:04:07 +0200

wallet-core: heuristically emit balance-change notification on tx transitions

We only emit the notifications when the major state changes.  This
simplifies the code and makes it harder to miss places where we should
emit the notification.

Diffstat:
Mpackages/taler-wallet-core/src/deposits.ts | 25+------------------------
Mpackages/taler-wallet-core/src/pay-merchant.ts | 6+-----
Mpackages/taler-wallet-core/src/pay-peer-pull-credit.ts | 6------
Mpackages/taler-wallet-core/src/pay-peer-pull-debit.ts | 13+++++--------
Mpackages/taler-wallet-core/src/pay-peer-push-credit.ts | 5-----
Mpackages/taler-wallet-core/src/pay-peer-push-debit.ts | 4----
Mpackages/taler-wallet-core/src/transactions.ts | 11+++++++++++
Mpackages/taler-wallet-core/src/withdraw.ts | 10----------
8 files changed, 18 insertions(+), 62 deletions(-)

diff --git a/packages/taler-wallet-core/src/deposits.ts b/packages/taler-wallet-core/src/deposits.ts @@ -432,10 +432,6 @@ export class DepositTransactionContext implements TransactionContext { wex.taskScheduler.stopShepherdTask(retryTag); notifyTransition(wex, transactionId, transitionInfo); wex.taskScheduler.startShepherdTask(retryTag); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: transactionId, - }); } async resumeTransaction(): Promise<void> { @@ -554,10 +550,6 @@ export class DepositTransactionContext implements TransactionContext { ); wex.taskScheduler.stopShepherdTask(taskId); notifyTransition(wex, transactionId, transitionInfo); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: transactionId, - }); } } @@ -915,10 +907,6 @@ async function waitForRefreshOnDepositGroup( ); notifyTransition(wex, ctx.transactionId, transitionInfo); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: ctx.transactionId, - }); return TaskRunResult.backoff(); } @@ -1445,10 +1433,6 @@ async function processDepositGroupPendingTrack( ); notifyTransition(wex, ctx.transactionId, transitionInfo); if (allWired) { - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: ctx.transactionId, - }); return TaskRunResult.finished(); } else { return TaskRunResult.longpollReturnedPending(); @@ -2213,20 +2197,13 @@ export async function createDepositGroup( }, ); - wex.ws.notify({ - type: NotificationType.TransactionStateTransition, - transactionId, + notifyTransition(wex, transactionId, { oldTxState: { major: TransactionMajorState.None, }, newTxState, }); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: transactionId, - }); - wex.taskScheduler.startShepherdTask(ctx.taskId); return { diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts @@ -2353,11 +2353,7 @@ export async function confirmPay( ); notifyTransition(wex, transactionId, transitionInfo); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: transactionId, - }); - + // In case we're sharing the payment and we're long-polling wex.taskScheduler.stopShepherdTask(ctx.taskId); diff --git a/packages/taler-wallet-core/src/pay-peer-pull-credit.ts b/packages/taler-wallet-core/src/pay-peer-pull-credit.ts @@ -1541,12 +1541,6 @@ export async function initiatePeerPullPayment( notifyTransition(wex, ctx.transactionId, transitionInfo); wex.taskScheduler.startShepherdTask(ctx.taskId); - // The pending-incoming balance has changed. - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: ctx.transactionId, - }); - return { talerUri: stringifyTalerUri({ type: TalerUriAction.PayPull, diff --git a/packages/taler-wallet-core/src/pay-peer-pull-debit.ts b/packages/taler-wallet-core/src/pay-peer-pull-debit.ts @@ -32,7 +32,6 @@ import { ExchangePurseDeposits, HttpStatusCode, Logger, - NotificationType, ObservabilityEventType, PeerContractTerms, PreparePeerPullDebitRequest, @@ -828,9 +827,7 @@ export async function confirmPeerPullDebit( const totalAmount = await getTotalPeerPaymentCost(wex, coins); - // FIXME: Missing notification here! - - await wex.db.runReadWriteTx( + const transitionInfo = await wex.db.runReadWriteTx( { storeNames: [ "coinAvailability", @@ -852,6 +849,7 @@ export async function confirmPeerPullDebit( if (pi.status !== PeerPullDebitRecordStatus.DialogProposed) { return; } + const oldTxState = computePeerPullDebitTransactionState(pi); if (coinSelRes.type == "success") { await spendCoins(wex, tx, { transactionId, @@ -870,13 +868,12 @@ export async function confirmPeerPullDebit( pi.status = PeerPullDebitRecordStatus.PendingDeposit; await ctx.updateTransactionMeta(tx); await tx.peerPullDebit.put(pi); + const newTxState = computePeerPullDebitTransactionState(pi); + return { oldTxState, newTxState }; }, ); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: transactionId, - }); + notifyTransition(wex, transactionId, transitionInfo); wex.taskScheduler.startShepherdTask(ctx.taskId); diff --git a/packages/taler-wallet-core/src/pay-peer-push-credit.ts b/packages/taler-wallet-core/src/pay-peer-push-credit.ts @@ -741,11 +741,6 @@ export async function preparePeerPushCredit( notifyTransition(wex, transactionId, transitionInfo); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: transactionId, - }); - return { amount: purseStatus.balance, amountEffective: wi.withdrawalAmountEffective, diff --git a/packages/taler-wallet-core/src/pay-peer-push-debit.ts b/packages/taler-wallet-core/src/pay-peer-push-debit.ts @@ -1354,10 +1354,6 @@ export async function initiatePeerPushDebit( }, ); notifyTransition(wex, transactionId, res.transitionInfo); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: transactionId, - }); wex.taskScheduler.startShepherdTask(ctx.taskId); diff --git a/packages/taler-wallet-core/src/transactions.ts b/packages/taler-wallet-core/src/transactions.ts @@ -671,5 +671,16 @@ export function notifyTransition( transactionId, experimentalUserData, }); + + // As a heuristic, we emit balance-change notifications + // whenever the major state changes. + // This sometimes emits more notifications than we need, + // but makes it much more unlikely that we miss any. + if (transitionInfo.newTxState.major !== transitionInfo.oldTxState.major) { + wex.ws.notify({ + type: NotificationType.BalanceChange, + hintTransactionId: transactionId, + }); + } } } diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts @@ -2395,11 +2395,6 @@ async function processWithdrawalGroupPendingReady( throw Error("withdrawal group does not exist anymore"); } - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: ctx.transactionId, - }); - if (numPlanchetErrors > 0) { return { type: TaskRunResultType.Error, @@ -3535,11 +3530,6 @@ export async function confirmWithdrawal( await wex.taskScheduler.resetTaskRetries(ctx.taskId); - wex.ws.notify({ - type: NotificationType.BalanceChange, - hintTransactionId: ctx.transactionId, - }); - const res = await wex.db.runReadWriteTx( { storeNames: ["exchanges"],