Skip to content

Commit

Permalink
fix: balance polling when closed
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w committed Feb 20, 2025
1 parent 376cdb4 commit 44c89b4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/background/services/balances/BalancePollingService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ describe('src/background/services/balances/BalancePollingService.ts', () => {
}
};

it('stops polling when extension is closed', async () => {
const service = new BalancePollingService(aggregatorServiceMock);

await service.startPolling(
account,
activeNetworkId,
roundRobinChainIds,
tokenTypes,
);

// Purposefully DO NOT await this, as we want to test the behavior
// of onAllExtensionsClosed() being called while polling is in progress.
runIntervalTimes(1);

expect(aggregatorServiceMock.getBalancesForNetworks).toHaveBeenCalledTimes(
2, // Once immediately after startPolling() and one more time after the first interval
);

service.onAllExtensionsClosed();

await runIntervalTimes(10); // Wait X intervals, 10 is arbitrary here
expect(aggregatorServiceMock.getBalancesForNetworks).toHaveBeenCalledTimes(
2, // Still only called twice. No more polling after onAllExtensionsClosed()
);
});

describe('when polling is active', () => {
beforeEach(async () => {
const service = new BalancePollingService(aggregatorServiceMock);
Expand Down
12 changes: 7 additions & 5 deletions src/background/services/balances/BalancePollingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export class BalancePollingService implements OnLock, OnAllExtensionClosed {
#timer: NodeJS.Timeout | null = null;
#pollingIteration = 0;
#lastPollingStartedAt?: number;

get isPollingActive() {
return this.#timer !== null;
}
#isPollingActive = false;

constructor(private balanceAggregator: BalanceAggregatorService) {}

Expand All @@ -35,6 +32,7 @@ export class BalancePollingService implements OnLock, OnAllExtensionClosed {
// Stop any polling that may be occurring already
this.stopPolling();
// Start a new interval
this.#isPollingActive = true;
return this.pollBalances(
account,
activeChainId,
Expand All @@ -48,6 +46,7 @@ export class BalancePollingService implements OnLock, OnAllExtensionClosed {
}

stopPolling() {
this.#isPollingActive = false;
if (this.#timer) {
clearTimeout(this.#timer);
this.#timer = null;
Expand Down Expand Up @@ -85,7 +84,10 @@ export class BalancePollingService implements OnLock, OnAllExtensionClosed {

// Only schedule the next update if another polling was not started
// while we were waiting for balance results.
if (thisPollingStartedAt === this.#lastPollingStartedAt) {
if (
this.#isPollingActive &&
thisPollingStartedAt === this.#lastPollingStartedAt
) {
this.scheduleNextUpdate(
account,
activeChainId,
Expand Down

0 comments on commit 44c89b4

Please sign in to comment.