Skip to content

Update PollingController to immediately call _executePoll if no polling is currently active for the key on start#1874

Merged
jiexi merged 19 commits intomainfrom
jl/add-immediate-execute-polling-controller
Oct 24, 2023
Merged

Update PollingController to immediately call _executePoll if no polling is currently active for the key on start#1874
jiexi merged 19 commits intomainfrom
jl/add-immediate-execute-polling-controller

Conversation

@jiexi
Copy link
Copy Markdown
Member

@jiexi jiexi commented Oct 19, 2023

Explanation

Currently when PollingController polling is started via startPollingByNetworkClientId(), the first poll execution doesn't occur until intervalLength time has passed. Some controllers such as the CurrencyRateController want to execute their polling logic immediately on polling start that way it is available to the UI as soon as possible. This PR adds a executeImmediately flag to the PollingController. When set to true, it causes _executePoll() to immediately be called when startPollingByNetworkClientId() is called, subsequently on each intervalLength time period. This value defaults to false. This PR updates #poll() to call _executePoll() if no polling is currently active for the key

References

Changelog

@metamask/polling-controller

  • BREAKING: _executePoll() is called immediately on start if no polling interval is already active for the networkClientId + options combination

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

@jiexi jiexi marked this pull request as ready for review October 19, 2023 21:13
@jiexi jiexi requested a review from a team as a code owner October 19, 2023 21:13
@jiexi jiexi changed the title Add executeImmediately flag to PollingController Update PollingController to call _executePoll if no polling is currently active for the key Oct 19, 2023
@jiexi jiexi changed the title Update PollingController to call _executePoll if no polling is currently active for the key Update PollingController to immediately call _executePoll if no polling is currently active for the key on start Oct 19, 2023
adonesky1
adonesky1 previously approved these changes Oct 23, 2023
Copy link
Copy Markdown
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Copy Markdown
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only one real comment on a pattern I see used in one test file that isn't used in other test files. Other than that my comments are more observational than actionable. It does seem like we are mostly writing tests a similar way so that's good.

data: sampleSepoliaTokensChainCache,
},
});
controller.stopPollingByPollingToken(pollingToken);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This test seems to do a lot. It's testing startPollingByNetworkClientId, but also stopPollingByPollingToken. I'm wondering if it's worth it to look at this test to see if it can be split up. I realize this is somewhat difficult, because in order to stop polling you have to start it somehow, so these methods seem linked. But I feel like this test would be hard to read for future maintainers. Nothing to do about this now, but something to think about as we increase how many controllers use this pattern.

@@ -117,7 +126,7 @@ describe('PollingController', () => {
});
});
describe('poll', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Typically, we use specific names for describes — that is, we name them after the methods that we are testing. Instead, this file seems to name describes more loosely. It seems like this would be confusing for anyone maintaining these tests, particularly poll here, as there is no method called "poll" (leaving the question of how these differ from start and stop). It would be nice to clean this up at some point.

Copy link
Copy Markdown
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@jiexi jiexi merged commit 9e00912 into main Oct 24, 2023
@jiexi jiexi deleted the jl/add-immediate-execute-polling-controller branch October 24, 2023 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants