From e90c3cfc2bc4aa752166ceee67d24e112575b99f Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Tue, 21 Jan 2025 16:14:10 +0530 Subject: [PATCH] fix(bg/PaymentSession): fix rate of pay, don't wait for prev payment --- src/background/services/paymentSession.ts | 45 +++++------------------ tsconfig.json | 1 + 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/src/background/services/paymentSession.ts b/src/background/services/paymentSession.ts index 8515dc82..d1cad11e 100644 --- a/src/background/services/paymentSession.ts +++ b/src/background/services/paymentSession.ts @@ -67,7 +67,6 @@ export class PaymentSession { private probingId: number; private shouldRetryImmediately = false; - private interval: ReturnType | null = null; private timeout: ReturnType | null = null; constructor( @@ -213,11 +212,6 @@ export class PaymentSession { } private clearTimers() { - if (this.interval) { - this.debug(`Clearing interval=${this.timeout}`); - clearInterval(this.interval); - this.interval = null; - } if (this.timeout) { this.debug(`Clearing timeout=${this.timeout}`); clearTimeout(this.timeout); @@ -257,45 +251,24 @@ export class PaymentSession { }); } - // Uncomment this after we perform the Rafiki test and remove the leftover - // code below. - // - // if (this.canContinuePayment) { - // this.timeout = setTimeout(() => { - // void this.payContinuous() - // - // this.interval = setInterval(() => { - // if (!this.canContinuePayment) { - // this.clearTimers() - // return - // } - // void this.payContinuous() - // }, this.intervalInMs) - // }, waitTime) - // } - - // Leftover const continuePayment = () => { if (!this.canContinuePayment) return; - // alternatively (leftover) after we perform the Rafiki test, we can just - // skip the `.then()` here and call setTimeout recursively immediately - void this.payContinuous().then(() => { - this.timeout = setTimeout( - () => { - continuePayment(); - }, - this.shouldRetryImmediately ? 0 : this.intervalInMs, - ); + void this.payContinuous().catch((err) => { + this.logger.error('Error while making continuous payment', err); }); + // This recursive call in setTimeout is essentially setInterval here, + // except we can have a dynamic interval (immediate vs intervalInMs). + this.timeout = setTimeout( + continuePayment, + this.shouldRetryImmediately ? 0 : this.intervalInMs, + ); }; if (this.canContinuePayment) { this.timeout = setTimeout(async () => { await this.payContinuous(); this.timeout = setTimeout( - () => { - continuePayment(); - }, + continuePayment, this.shouldRetryImmediately ? 0 : this.intervalInMs, ); }, waitTime); diff --git a/tsconfig.json b/tsconfig.json index ceed0dfc..a24aae7c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,7 @@ "allowSyntheticDefaultImports": true, "allowUmdGlobalAccess": true, "resolveJsonModule": true, + "noUnusedLocals": true, "noEmit": true, "paths": { "@/shared/*": ["./shared/*"],