Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added options to PollingController to help handle cases where the Controller need more than just networkClientId #1776

Merged
merged 19 commits into from
Oct 11, 2023

Conversation

shanejonas
Copy link
Contributor

Explanation

Adds options parameter passed around to allow a unique key to be derived per networkClientId + option combination. This handles cases where a Controller need more than just networkClientId to poll on, like an address for example.

References

Fixes https://github.com/MetaMask/MetaMask-planning/issues/1406

Changelog

@metamask/polling-controller

  • ADDED: options to PollingController Mixin to help handle cases where a Controller need more than just networkClientId

@shanejonas shanejonas requested a review from a team as a code owner October 4, 2023 20:43
Copy link
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. A few small nits. Still would like approval from @mcmire before we merge.

Copy link
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.

My main question is around the name we want to choose for the keys in this.#networkClientIdTokensMap (and other places), however, I'd also like to see the options receive a more accurate type (or at the very least, a consistent type instead of sometimes any and sometimes object).

packages/polling-controller/src/PollingController.test.ts Outdated Show resolved Hide resolved
packages/polling-controller/src/PollingController.ts Outdated Show resolved Hide resolved
packages/polling-controller/src/PollingController.ts Outdated Show resolved Hide resolved
packages/polling-controller/src/PollingController.ts Outdated Show resolved Hide resolved
packages/polling-controller/src/PollingController.ts Outdated Show resolved Hide resolved
packages/polling-controller/src/PollingController.ts Outdated Show resolved Hide resolved
packages/polling-controller/src/PollingController.ts Outdated Show resolved Hide resolved
packages/polling-controller/src/PollingController.ts Outdated Show resolved Hide resolved
adonesky1
adonesky1 previously approved these changes Oct 11, 2023
Copy link
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.

Still wondering if we could add another test here (per this comment) but otherwise LGTM. Maybe lets do Elliot one last pass before merging.

Copy link
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 feel like there's still a bit of confusion on what things hold what things and what they should be called. I have a couple more comments on that.

@@ -20,10 +34,9 @@ function PollingControllerMixin<TBase extends Constructor>(Base: TBase) {
*
*/
abstract class PollingControllerBase extends Base {
readonly #networkClientIdTokensMap: Map<NetworkClientId, Set<string>> =
new Map();
readonly #pollingGroupIds: Map<PollingGroupId, Set<string>> = new Map();
Copy link
Contributor

@mcmire mcmire Oct 11, 2023

Choose a reason for hiding this comment

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

pollingGroupIds sounds like this variable holds an array of ids. What do you think about:

Suggested change
readonly #pollingGroupIds: Map<PollingGroupId, Set<string>> = new Map();
readonly #pollTokenGroups: Map<PollingGroupId, Set<string>> = new Map();

On the other hand, it seems like when we use this variable we really care about the poll tokens. Also, intervalIds and callbacks seem to be named after the values that they hold, and so maybe it makes sense for this variable to follow the same pattern:

Suggested change
readonly #pollingGroupIds: Map<PollingGroupId, Set<string>> = new Map();
readonly #pollTokens: Map<PollingGroupId, Set<string>> = new Map();


const key = getKey(networkClientId, options);

const pollingGroupId = this.#pollingGroupIds.get(key);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this right? I thought each value in #pollingGroupIds is set of poll tokens. It seems that key here is really the polling group ID.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah currently the type you have returned for getKey is PollingGroupId, so presumably you'd use the pollingGroupId to get a pollingGroup or pollingTokenGroup or something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea really it should be pollingTokenSet, and #pollingGroupIds should be #pollingTokenSets or #pollingTokensByPollingGroupId

Copy link
Contributor

@adonesky1 adonesky1 Oct 11, 2023

Choose a reason for hiding this comment

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

pollingTokenSet

👍

#pollingTokenSets

I'd vote either this or just pollingTokens per @mcmire 's comment

Copy link
Contributor

Choose a reason for hiding this comment

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

probably pollingTokenSets though since it would be kindof weird for pollingTokenSet to be a subset of pollingTokens?

Copy link
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! Sorry for all of the back and forth.

Copy link
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.

🎉

options: Json,
): PollingGroupId => `${networkClientId}:${stringify(options)}`;

type PollingGroupId = `${NetworkClientId}:${string}`;
Copy link
Contributor

@adonesky1 adonesky1 Oct 11, 2023

Choose a reason for hiding this comment

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

[nit] last thing is maybe we should align with the pollingTokenSet verbiage -> PollingTokenSetId?

@adonesky1 adonesky1 merged commit d53d83a into main Oct 11, 2023
107 checks passed
@adonesky1 adonesky1 deleted the polling-controller-generic-options branch October 11, 2023 21:20
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
…troller need more than just networkClientId (#1776)

## Explanation
Adds `options` parameter passed around to allow a unique key to be
derived per networkClientId + option combination. This handles cases
where a Controller need more than just networkClientId to poll on, like
an `address` for example.

Fixes MetaMask/MetaMask-planning#1406

## Changelog

### `@metamask/polling-controller`
- **ADDED**: options to PollingController Mixin to help handle cases
where a Controller need more than just networkClientId

---------

Co-authored-by: Alex Donesky <adonesky@gmail.com>
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
…troller need more than just networkClientId (#1776)

## Explanation
Adds `options` parameter passed around to allow a unique key to be
derived per networkClientId + option combination. This handles cases
where a Controller need more than just networkClientId to poll on, like
an `address` for example.

Fixes MetaMask/MetaMask-planning#1406

## Changelog

### `@metamask/polling-controller`
- **ADDED**: options to PollingController Mixin to help handle cases
where a Controller need more than just networkClientId

---------

Co-authored-by: Alex Donesky <adonesky@gmail.com>
MajorLift pushed a commit that referenced this pull request Oct 12, 2023
…troller need more than just networkClientId (#1776)

## Explanation
Adds `options` parameter passed around to allow a unique key to be
derived per networkClientId + option combination. This handles cases
where a Controller need more than just networkClientId to poll on, like
an `address` for example.

Fixes MetaMask/MetaMask-planning#1406

## Changelog

### `@metamask/polling-controller`
- **ADDED**: options to PollingController Mixin to help handle cases
where a Controller need more than just networkClientId

---------

Co-authored-by: Alex Donesky <adonesky@gmail.com>
adonesky1 added a commit that referenced this pull request Oct 12, 2023
…d` -> `stopPollingByPollingToken` (#1810)

Fast-follow to #1776, just a few
(breaking) naming cleanups

### CHANGED
- **BREAKING:** `executePoll` renamed to `_executePoll`
- **BREAKING:** `stopPollingByNetworkClientId` renamed to
`stopPollingByPollingToken`
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