Skip to content

Commit 734a77e

Browse files
authored
Merge branch 'main' into feat/move-multichain-account-impl
2 parents 0e8df7b + b407773 commit 734a77e

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

packages/accounts-controller/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Stop updating `selectedAccount` unnecesarily ([#6218](https://github.com/MetaMask/core/pull/6218))
13+
1014
## [32.0.1]
1115

1216
### Fixed

packages/accounts-controller/src/AccountsController.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,35 @@ describe('AccountsController', () => {
18041804
mockOlderEvmAccount.id,
18051805
);
18061806
});
1807+
1808+
it('should not emit an update if the selected account does not change', () => {
1809+
const messenger = buildMessenger();
1810+
const spy = jest.spyOn(messenger, 'publish');
1811+
const { accountsController, triggerMultichainNetworkChange } =
1812+
setupAccountsController({
1813+
initialState: {
1814+
internalAccounts: {
1815+
accounts: {
1816+
[mockOlderEvmAccount.id]: mockOlderEvmAccount,
1817+
},
1818+
selectedAccount: mockOlderEvmAccount.id,
1819+
},
1820+
},
1821+
messenger,
1822+
});
1823+
1824+
triggerMultichainNetworkChange(InfuraNetworkType.mainnet);
1825+
1826+
expect(spy).not.toHaveBeenCalledWith(
1827+
'AccountsController:stateChange',
1828+
expect.any(Object),
1829+
expect.any(Array),
1830+
);
1831+
1832+
expect(accountsController.state.internalAccounts.selectedAccount).toBe(
1833+
mockOlderEvmAccount.id,
1834+
);
1835+
});
18071836
});
18081837

18091838
describe('updateAccounts', () => {

packages/accounts-controller/src/AccountsController.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ export class AccountsController extends BaseController<
428428
setSelectedAccount(accountId: string): void {
429429
const account = this.getAccountExpect(accountId);
430430

431+
if (this.state.internalAccounts.selectedAccount === account.id) {
432+
return;
433+
}
434+
431435
this.#update((state) => {
432436
const { internalAccounts } = state;
433437

@@ -1171,6 +1175,10 @@ export class AccountsController extends BaseController<
11711175
accountId = lastSelectedEvmAccount.id;
11721176
}
11731177

1178+
if (this.state.internalAccounts.selectedAccount === accountId) {
1179+
return;
1180+
}
1181+
11741182
this.update((currentState) => {
11751183
currentState.internalAccounts.accounts[accountId].metadata.lastSelected =
11761184
Date.now();

0 commit comments

Comments
 (0)