Conversation
BelfordZ
approved these changes
Dec 1, 2023
…WillChange-networkDidChange
13 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
While debugging an issue with SwapsController's provider instance being updated too soon due to the remove networkId refactor, I noticed many of our controllers listen to
NetworkController:stateChangewhich fires numerous times each time the network switches. Some of these controllers keep track of thechainIdit's last seen between these events to ensure it only fires off any necessary side effects once. Some controllers don't keep track of if it's seen thechainIdat all and will fire off the side effects for each and everystateChangeevent. UsingchainIdas a guard isn't even necessarily correct in some cases as the provider may have changed despite the chainId remaining the same.It seems more appropriate/accurate for these controllers to be listening to
NetworkController:networkDidChangeinstead which only fires once for each network switch, and after the provider proxy has been updated to point at the right RPC endpoint.Currently the
NetworkController:networkDidChangeevent is a little bit inconvenient to consume because generally any listeners to this event would want to know what the network changed to / what the current state of things are to determine next steps. This PR addsNetworkStateto theNetworkController:networkWillChangeandNetworkController:networkDidChangeevent payloadsReferences
Changelog
@metamask/network-controllerNetworkController:networkWillChangeincludesNetworkStateas the first and only item in the payloadNetworkController:networkDidChangeincludesNetworkStateas the first and only item in the payloadChecklist