Skip to content

Commit

Permalink
fix(bg/PaymentSession): fix rate of pay, don't wait for prev payment
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Jan 21, 2025
1 parent 6aa617b commit e90c3cf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 36 deletions.
45 changes: 9 additions & 36 deletions src/background/services/paymentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class PaymentSession {
private probingId: number;
private shouldRetryImmediately = false;

private interval: ReturnType<typeof setInterval> | null = null;
private timeout: ReturnType<typeof setTimeout> | null = null;

constructor(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"allowSyntheticDefaultImports": true,
"allowUmdGlobalAccess": true,
"resolveJsonModule": true,
"noUnusedLocals": true,
"noEmit": true,
"paths": {
"@/shared/*": ["./shared/*"],
Expand Down

0 comments on commit e90c3cf

Please sign in to comment.