Skip to content

Commit ec94c9b

Browse files
authored
fix: process first call to update the rates controller then start polling (#5364)
## Explanation We observed that once the rate controller is initialized, polling does not begin until the default interval of three minutes has passed, causing the rate to only be retrieved after that delay. This leads to a suboptimal user experience. This pull request addresses the issue by manually triggering a rate update. <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/assets-controllers` - **FIXED**: Resolved an issue where rate polling would only begin after the default 3-minute interval by manually triggering a rate update upon initialization, ensuring an immediate refresh for a better user experience ## Checklist - [x] 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
1 parent 02a504d commit ec94c9b

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

packages/assets-controllers/src/RatesController/RatesController.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('RatesController', () => {
170170
const ratesPosUpdate = ratesController.state.rates;
171171

172172
// checks for the RatesController:stateChange event
173-
expect(publishActionSpy).toHaveBeenCalledTimes(2);
173+
expect(publishActionSpy).toHaveBeenCalledTimes(3);
174174
expect(fetchExchangeRateStub).toHaveBeenCalled();
175175
expect(ratesPosUpdate).toStrictEqual({
176176
btc: {
@@ -283,27 +283,27 @@ describe('RatesController', () => {
283283

284284
await advanceTime({ clock, duration: 200 });
285285

286-
expect(fetchExchangeRateStub).toHaveBeenCalledTimes(1);
286+
expect(fetchExchangeRateStub).toHaveBeenCalledTimes(2);
287287

288288
await ratesController.stop();
289289

290290
// check the 3rd call since the 2nd one is for the
291291
// event stateChange
292292
expect(publishActionSpy).toHaveBeenNthCalledWith(
293-
3,
293+
4,
294294
`${ratesControllerName}:pollingStopped`,
295295
);
296296

297297
await advanceTime({ clock, duration: 200 });
298298

299-
expect(fetchExchangeRateStub).toHaveBeenCalledTimes(1);
299+
expect(fetchExchangeRateStub).toHaveBeenCalledTimes(2);
300300

301301
await ratesController.stop();
302302

303303
// check if the stop method is called again, it returns early
304304
// and no extra logic is executed
305305
expect(publishActionSpy).not.toHaveBeenNthCalledWith(
306-
4,
306+
3,
307307
`${ratesControllerName}:pollingStopped`,
308308
);
309309
});

packages/assets-controllers/src/RatesController/RatesController.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ export class RatesController extends BaseController<
167167

168168
this.messagingSystem.publish(`${name}:pollingStarted`);
169169

170+
await this.#updateRates();
171+
170172
this.#intervalId = setInterval(() => {
171173
this.#executePoll().catch(console.error);
172174
}, this.#intervalLength);

0 commit comments

Comments
 (0)