Skip to content

Commit

Permalink
fix: issue where QueuedRequestController.state.queuedRequestCount i…
Browse files Browse the repository at this point in the history
…s not updated after flushing requests for an origin (#4898)

## Explanation

Fix issue where `QueuedRequestController.state.queuedRequestCount` is
not updated after flushing requests for an origin

## References
- #4846
- MetaMask/metamask-extension#28090

## Fixes
- Bug identified [in v12.7.0 RC
Thread](https://consensys.slack.com/archives/C029JG63136/p1730918073046389?thread_ts=1729246801.516029&cid=C029JG63136)

## Before


https://drive.google.com/file/d/1ujdQgVLlT8KlwRwO-Cc3XvRHPrkpxIg_/view?usp=drive_link

## After


https://github.com/user-attachments/assets/e77928e5-165b-441a-b4da-0e10471c0529


## Changelog

### `@metamask/queued-request-controller`

- **FIX**: Ensure queuedRequestCount value is updated properly after
flushing the queue for a given origin

## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
  • Loading branch information
adonesky1 authored Nov 6, 2024
1 parent b27a5d7 commit 340f183
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,58 @@ describe('QueuedRequestController', () => {
expect(controller.state).toStrictEqual({ queuedRequestCount: 0 });
});

it('updates queuedRequestCount when flushing requests for an origin', async () => {
const { messenger } = buildControllerMessenger();
const controller = new QueuedRequestController({
messenger: buildQueuedRequestControllerMessenger(messenger),
shouldRequestSwitchNetwork: () => false,
canRequestSwitchNetworkWithoutApproval: () => false,
clearPendingConfirmations: jest.fn(),
showApprovalRequest: jest.fn(),
});

const firstRequest = controller.enqueueRequest(
{ ...buildRequest(), origin: 'https://example.com' },
() => Promise.resolve(),
);
const secondRequest = controller.enqueueRequest(
{ ...buildRequest(), origin: 'https://example2.com' },
() => Promise.resolve(),
);
const thirdRequest = controller.enqueueRequest(
{ ...buildRequest(), origin: 'https://example2.com' },
() => Promise.resolve(),
);

expect(controller.state.queuedRequestCount).toBe(2);

// When the selected network changes for a domain, the queued requests for that domain/origin are flushed
messenger.publish(
'SelectedNetworkController:stateChange',
{ domains: {} },
[
{
op: 'replace',
path: ['domains', 'https://example2.com'],
},
],
);

expect(controller.state.queuedRequestCount).toBe(0);

await firstRequest;
await expect(secondRequest).rejects.toThrow(
new Error(
'The request has been rejected due to a change in selected network. Please verify the selected network and retry the request.',
),
);
await expect(thirdRequest).rejects.toThrow(
new Error(
'The request has been rejected due to a change in selected network. Please verify the selected network and retry the request.',
),
);
});

describe('enqueueRequest', () => {
it('throws an error if networkClientId is not provided', async () => {
const controller = buildQueuedRequestController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export class QueuedRequestController extends BaseController<
this.#requestQueue = this.#requestQueue.filter(
({ request }) => request.origin !== flushOrigin,
);
this.#updateQueuedRequestCount();
}

/**
Expand Down

0 comments on commit 340f183

Please sign in to comment.