diff options
-rw-r--r-- | packages/taler-wallet-core/src/withdraw.ts | 119 |
1 files changed, 46 insertions, 73 deletions
diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts index 16289b1ef..1dc4e0999 100644 --- a/packages/taler-wallet-core/src/withdraw.ts +++ b/packages/taler-wallet-core/src/withdraw.ts @@ -114,6 +114,7 @@ import { TransitionResult, TransitionResultType, constructTaskIdentifier, + genericWaitForState, makeCoinAvailable, makeCoinsVisible, } from "./common.js"; @@ -3207,88 +3208,60 @@ export async function acceptWithdrawalFromUri( }; } -async function internalWaitWithdrawalRegistered( +async function waitWithdrawalRegistered( wex: WalletExecutionContext, ctx: WithdrawTransactionContext, - withdrawalNotifFlag: AsyncFlag, ): Promise<void> { - while (true) { - const { withdrawalRec, retryRec } = await wex.db.runReadOnlyTx( - { storeNames: ["withdrawalGroups", "operationRetries"] }, - async (tx) => { - return { - withdrawalRec: await tx.withdrawalGroups.get(ctx.withdrawalGroupId), - retryRec: await tx.operationRetries.get(ctx.taskId), - }; - }, - ); + await genericWaitForState(wex, { + async checkState(): Promise<boolean> { + const { withdrawalRec, retryRec } = await wex.db.runReadOnlyTx( + { storeNames: ["withdrawalGroups", "operationRetries"] }, + async (tx) => { + return { + withdrawalRec: await tx.withdrawalGroups.get(ctx.withdrawalGroupId), + retryRec: await tx.operationRetries.get(ctx.taskId), + }; + }, + ); - if (!withdrawalRec) { - throw Error("withdrawal not found anymore"); - } + if (!withdrawalRec) { + throw Error("withdrawal not found anymore"); + } - switch (withdrawalRec.status) { - case WithdrawalGroupStatus.FailedBankAborted: - throw TalerError.fromDetail( - TalerErrorCode.WALLET_WITHDRAWAL_OPERATION_ABORTED_BY_BANK, - {}, - ); - case WithdrawalGroupStatus.PendingKyc: - case WithdrawalGroupStatus.PendingAml: - case WithdrawalGroupStatus.PendingQueryingStatus: - case WithdrawalGroupStatus.PendingReady: - case WithdrawalGroupStatus.Done: - case WithdrawalGroupStatus.PendingWaitConfirmBank: - return; - case WithdrawalGroupStatus.PendingRegisteringBank: - break; - default: { - if (retryRec) { - if (retryRec.lastError) { - throw TalerError.fromUncheckedDetail(retryRec.lastError); - } else { - throw Error("withdrawal unexpectedly pending"); + switch (withdrawalRec.status) { + case WithdrawalGroupStatus.FailedBankAborted: + throw TalerError.fromDetail( + TalerErrorCode.WALLET_WITHDRAWAL_OPERATION_ABORTED_BY_BANK, + {}, + ); + case WithdrawalGroupStatus.PendingKyc: + case WithdrawalGroupStatus.PendingAml: + case WithdrawalGroupStatus.PendingQueryingStatus: + case WithdrawalGroupStatus.PendingReady: + case WithdrawalGroupStatus.Done: + case WithdrawalGroupStatus.PendingWaitConfirmBank: + return true; + case WithdrawalGroupStatus.PendingRegisteringBank: + break; + default: { + if (retryRec) { + if (retryRec.lastError) { + throw TalerError.fromUncheckedDetail(retryRec.lastError); + } else { + throw Error("withdrawal unexpectedly pending"); + } } } } - } - - await withdrawalNotifFlag.wait(); - withdrawalNotifFlag.reset(); - } -} - -async function waitWithdrawalRegistered( - wex: WalletExecutionContext, - ctx: WithdrawTransactionContext, -): Promise<void> { - // FIXME: Doesn't support cancellation yet - // FIXME: We should use Symbol.dispose magic here for cleanup! - - const withdrawalNotifFlag = new AsyncFlag(); - // Raise exchangeNotifFlag whenever we get a notification - // about our exchange. - const cancelNotif = wex.ws.addNotificationListener((notif) => { - if ( - notif.type === NotificationType.TransactionStateTransition && - notif.transactionId === ctx.transactionId - ) { - logger.info(`raising update notification: ${j2s(notif)}`); - withdrawalNotifFlag.raise(); - } + return false; + }, + filterNotification(notif) { + return ( + notif.type === NotificationType.TransactionStateTransition && + notif.transactionId === ctx.transactionId + ); + }, }); - - try { - const res = await internalWaitWithdrawalRegistered( - wex, - ctx, - withdrawalNotifFlag, - ); - logger.info("done waiting for ready exchange"); - return res; - } finally { - cancelNotif(); - } } async function fetchAccount( |