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

Commit

Permalink
Fix #8750: Accounts tab add account race condition (#8751)
Browse files Browse the repository at this point in the history
Fix race condition when an account is added while balances & prices are being fetched
  • Loading branch information
StephenHeaps authored Feb 8, 2024
1 parent 1f7fe69 commit 0d7f871
Showing 1 changed file with 9 additions and 5 deletions.
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

0 comments on commit 0d7f871

Please sign in to comment.