Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #8750: Accounts tab add account race condition #8751

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Sources/BraveWallet/Crypto/Stores/AccountsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ class AccountsStore: ObservableObject, WalletObserverStore {
walletServiceObserver = nil
}

private var updateTask: Task<(), Never>?
func update() {
Task { @MainActor in
updateTask?.cancel()
updateTask = Task { @MainActor in
self.isLoading = true
defer { self.isLoading = false }

let allAccounts = await keyringService.allAccounts()
let allNetworks = await rpcService.allNetworksForSupportedCoins()
let allTokensPerNetwork = userAssetManager.getAllUserAssetsInNetworkAssets(
networks: allNetworks,
Expand All @@ -107,18 +108,21 @@ class AccountsStore: ObservableObject, WalletObserverStore {
}
let tokens = allTokensPerNetwork.flatMap(\.tokens)

var accountDetails = buildAccountDetails(accounts: allAccounts.accounts, tokens: tokens)
var allAccounts = await keyringService.allAccounts().accounts
var accountDetails = buildAccountDetails(accounts: allAccounts, tokens: tokens)
self.primaryAccounts = accountDetails
.filter(\.account.isPrimary)
self.importedAccounts = accountDetails
.filter(\.account.isImported)

await updateBalancesAndPrices(
for: allAccounts.accounts,
for: allAccounts,
networkAssets: allTokensPerNetwork
)

accountDetails = buildAccountDetails(accounts: allAccounts.accounts, tokens: tokens)
// if new accounts added while balances were being fetched.
allAccounts = await keyringService.allAccounts().accounts
accountDetails = buildAccountDetails(accounts: allAccounts, tokens: tokens)
self.primaryAccounts = accountDetails
.filter(\.account.isPrimary)
self.importedAccounts = accountDetails
Expand Down
Loading