From 52a1f63e0a8cc2ca78910e8b56326376eb1d75d0 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 21 Feb 2024 14:23:01 +0100 Subject: wallet-core: use cancellation tokens when possible --- .../taler-wallet-core/src/pay-peer-pull-credit.ts | 37 +++++++++++++++------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'packages/taler-wallet-core/src/pay-peer-pull-credit.ts') diff --git a/packages/taler-wallet-core/src/pay-peer-pull-credit.ts b/packages/taler-wallet-core/src/pay-peer-pull-credit.ts index e764d2169..7774dfd5f 100644 --- a/packages/taler-wallet-core/src/pay-peer-pull-credit.ts +++ b/packages/taler-wallet-core/src/pay-peer-pull-credit.ts @@ -54,7 +54,7 @@ import { import { readSuccessResponseJsonOrThrow } from "@gnu-taler/taler-util/http"; import { PendingTaskType, - TaskId, + TaskIdStr, TaskRunResult, TaskRunResultType, TombstoneTag, @@ -91,13 +91,13 @@ const logger = new Logger("pay-peer-pull-credit.ts"); export class PeerPullCreditTransactionContext implements TransactionContext { readonly transactionId: TransactionIdStr; - readonly retryTag: TaskId; + readonly taskId: TaskIdStr; constructor( public ws: InternalWalletState, public pursePub: string, ) { - this.retryTag = constructTaskIdentifier({ + this.taskId = constructTaskIdentifier({ tag: PendingTaskType.PeerPullCredit, pursePub, }); @@ -138,7 +138,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { } async suspendTransaction(): Promise { - const { ws, pursePub, retryTag, transactionId } = this; + const { ws, pursePub, taskId: retryTag, transactionId } = this; const transitionInfo = await ws.db.runReadWriteTx( ["peerPullCredit"], async (tx) => { @@ -198,7 +198,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { } async failTransaction(): Promise { - const { ws, pursePub, retryTag, transactionId } = this; + const { ws, pursePub, taskId: retryTag, transactionId } = this; const transitionInfo = await ws.db.runReadWriteTx( ["peerPullCredit"], async (tx) => { @@ -249,7 +249,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { } async resumeTransaction(): Promise { - const { ws, pursePub, retryTag, transactionId } = this; + const { ws, pursePub, taskId: retryTag, transactionId } = this; const transitionInfo = await ws.db.runReadWriteTx( ["peerPullCredit"], async (tx) => { @@ -308,7 +308,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { } async abortTransaction(): Promise { - const { ws, pursePub, retryTag, transactionId } = this; + const { ws, pursePub, taskId: retryTag, transactionId } = this; const transitionInfo = await ws.db.runReadWriteTx( ["peerPullCredit"], async (tx) => { @@ -529,6 +529,7 @@ async function longpollKycStatus( async function processPeerPullCreditAbortingDeletePurse( ws: InternalWalletState, peerPullIni: PeerPullCreditRecord, + cancellationToken: CancellationToken, ): Promise { const { pursePub, pursePriv } = peerPullIni; const transactionId = constructTransactionIdentifier({ @@ -545,6 +546,7 @@ async function processPeerPullCreditAbortingDeletePurse( headers: { "taler-purse-signature": sigResp.sig, }, + cancellationToken, }); logger.info(`deleted purse with response status ${resp.status}`); @@ -637,6 +639,7 @@ async function handlePeerPullCreditWithdrawing( async function handlePeerPullCreditCreatePurse( ws: InternalWalletState, pullIni: PeerPullCreditRecord, + cancellationToken: CancellationToken, ): Promise { const purseFee = Amounts.stringify(Amounts.zeroOfAmount(pullIni.amount)); const pursePub = pullIni.pursePub; @@ -717,13 +720,19 @@ async function handlePeerPullCreditCreatePurse( const httpResp = await ws.http.fetch(reservePurseMergeUrl.href, { method: "POST", body: reservePurseReqBody, + cancellationToken, }); if (httpResp.status === HttpStatusCode.UnavailableForLegalReasons) { const respJson = await httpResp.json(); const kycPending = codecForWalletKycUuid().decode(respJson); logger.info(`kyc uuid response: ${j2s(kycPending)}`); - return processPeerPullCreditKycRequired(ws, pullIni, kycPending); + return processPeerPullCreditKycRequired( + ws, + pullIni, + kycPending, + cancellationToken, + ); } const resp = await readSuccessResponseJsonOrThrow(httpResp, codecForAny()); @@ -792,9 +801,13 @@ export async function processPeerPullCredit( ); } case PeerPullPaymentCreditStatus.PendingCreatePurse: - return handlePeerPullCreditCreatePurse(ws, pullIni); + return handlePeerPullCreditCreatePurse(ws, pullIni, cancellationToken); case PeerPullPaymentCreditStatus.AbortingDeletePurse: - return await processPeerPullCreditAbortingDeletePurse(ws, pullIni); + return await processPeerPullCreditAbortingDeletePurse( + ws, + pullIni, + cancellationToken, + ); case PeerPullPaymentCreditStatus.PendingWithdrawing: return handlePeerPullCreditWithdrawing(ws, pullIni); case PeerPullPaymentCreditStatus.Aborted: @@ -817,6 +830,7 @@ async function processPeerPullCreditKycRequired( ws: InternalWalletState, peerIni: PeerPullCreditRecord, kycPending: WalletKycUuid, + cancellationToken: CancellationToken, ): Promise { const transactionId = constructTransactionIdentifier({ tag: TransactionType.PeerPullCredit, @@ -833,6 +847,7 @@ async function processPeerPullCreditKycRequired( logger.info(`kyc url ${url.href}`); const kycStatusRes = await ws.http.fetch(url.href, { method: "GET", + cancellationToken, }); if ( @@ -1080,7 +1095,7 @@ export async function initiatePeerPullPayment( }); notifyTransition(ws, ctx.transactionId, transitionInfo); - ws.taskScheduler.startShepherdTask(ctx.retryTag); + ws.taskScheduler.startShepherdTask(ctx.taskId); return { talerUri: stringifyTalerUri({ -- cgit v1.2.3