-
-
Notifications
You must be signed in to change notification settings - Fork 255
fix(accounts-controller): fire :accountAdded before :selectedAccountChange on KeyringController:stateChange
#6567
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
fix(accounts-controller): fire :accountAdded before :selectedAccountChange on KeyringController:stateChange
#6567
Conversation
…hange on KeyringController:stateChange
9b3135c to
1f2cfed
Compare
|
@metamaskbot publish-preview |
|
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions. |
| // Will get executed after the update, but before re-selecting an account in case | ||
| // the current one is not valid anymore. | ||
| () => { | ||
| // Now publish events | ||
| for (const id of diff.removed) { | ||
| this.messagingSystem.publish('AccountsController:accountRemoved', id); | ||
| } | ||
|
|
||
| for (const account of diff.added) { | ||
| this.messagingSystem.publish('AccountsController:accountAdded', account); | ||
| } | ||
| for (const account of diff.added) { | ||
| this.messagingSystem.publish( | ||
| 'AccountsController:accountAdded', | ||
| account, | ||
| ); | ||
| } | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new callback will get executed prior to the account selection. This solves the issue during onboarding when the keyring creates its initial account (which triggers a :stateChange).
With this change, we will be firing :accountAdded before :selectedAccount, thus allowing the account-tree-controller to first create its group before trying to mirror the selected account group.
|
|
||
| this.#update((state) => { | ||
| const { internalAccounts } = state; | ||
| this.#update( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Watchout, this call now takes 2 callbacks:
- The actual update to run
- The pre-step before selecting the new account that can result from the state update
mathieuartu
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, have manually tested in extension and works as intended
Explanation
We were firing
:selectedAccountChangebefore firing:accountAddedwhich does have undesired side-effect in theAccountTreeController.Mainly because the tree will try to get the associated selected account group, but this account group does not exist yet (since it's using
:accountAddedto create the account groups).This is more like a temporary solution, and
#updatecould be slightly refactored to a more robust solution later on.References
N/A
Checklist