fix: SelectedNetworkController network stateChange listener#4638
Merged
mikesposito merged 5 commits intogroup-network-configurations-by-chainfrom Sep 2, 2024
Conversation
SelectedNetworkController expected network stateChange patchSelectedNetworkController network stateChange listener
290f23a to
e0fda92
Compare
Contributor
|
product change looks good, ive tested it via extension. Would be good to merge this to the main PR so we can publish a new preview release. Edit: I committed it to the main PR for that reason |
Member
Author
|
New dependencies detected. Learn more about Socket for GitHub ↗︎
|
mikesposito
commented
Aug 28, 2024
Comment on lines
+195
to
+207
| (availableNetworkClientIds) => { | ||
| // if a network is updated or removed, update the networkClientId for all domains | ||
| // that were using it to the selected network client id | ||
| const { selectedNetworkClientId } = this.messagingSystem.call( | ||
| 'NetworkController:getState', | ||
| ); | ||
| Object.entries(this.state.domains).forEach( | ||
| ([domain, networkClientIdForDomain]) => { | ||
| if (!availableNetworkClientIds.includes(networkClientIdForDomain)) { | ||
| this.setNetworkClientIdForDomain(domain, selectedNetworkClientId); | ||
| } | ||
| }, | ||
| ); |
Member
Author
There was a problem hiding this comment.
The usage of immer patches has been removed since it strictly depends on how state changes are made on the controller that originates the event.
We can reduce the number of events received by this listener using a selector
mikesposito
commented
Aug 28, 2024
aa8d6f7 to
e050d07
Compare
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
Currently,
SelectedNetworkControllersubscribes to theNetworkController:stateChangeevent to make sure that network client ids associated with managed domains stay valid. Though, we are currently checking on the specific immer patch applied to the NetworkController state: this means that based on how the state draft was manipulated on the origin controller, we may or may not capture the change and react to it.To fix it, now
SelectedNetworkControllerwill look at the actual content of the updated state, instead of relying on immer patches. To reduce the amount of event instances received by the controller, a selector is applied to the subscription, filtering out irrelevant instances.References
Changelog
@metamask/network-controllergetNetworkConfigurations,getAvailableNetworkClientIdsandselectAvailableNetworkClientIdsselectors@metamask/selected-network-controllerNo consumer-faced changes
Checklist