Skip to content
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

[ABW-2271] Remove spurious portfolio reloading #829

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ extension AccountPortfoliosClient: DependencyKey {
actor State {
let portfoliosSubject: AsyncCurrentValueSubject<[AccountAddress: AccountPortfolio]> = .init([:])

func setAccountPortfolio(_ portfolio: AccountPortfolio) {
func setOrUpdateAccountPortfolio(_ portfolio: AccountPortfolio) {
portfoliosSubject.value.updateValue(portfolio, forKey: portfolio.owner)
}

func setAccountPortfolios(_ portfolios: [AccountPortfolio]) {
portfolios.forEach(setAccountPortfolio)
func setOrUpdateAccountPortfolios(_ portfolios: [AccountPortfolio]) {
var newValue = portfoliosSubject.value
for portfolio in portfolios {
newValue[portfolio.owner] = portfolio
}
portfoliosSubject.value = newValue
}

func portfolioForAccount(_ address: AccountAddress) -> AnyAsyncSequence<AccountPortfolio> {
portfoliosSubject.compactMap { $0[address] }.eraseToAnyAsyncSequence()
portfoliosSubject.compactMap { $0[address] }.removeDuplicates().eraseToAnyAsyncSequence()
}
}

Expand All @@ -36,7 +40,7 @@ extension AccountPortfoliosClient: DependencyKey {
request: { try await AccountPortfoliosClient.fetchAccountPortfolio(accountAddress) }
)

await state.setAccountPortfolio(portfolio)
await state.setOrUpdateAccountPortfolio(portfolio)

return portfolio
}
Expand Down Expand Up @@ -76,7 +80,7 @@ extension AccountPortfoliosClient: DependencyKey {
}()

// Update the current account portfolios
await state.setAccountPortfolios(portfolios)
await state.setOrUpdateAccountPortfolios(portfolios)

return portfolios
},
Expand All @@ -87,7 +91,7 @@ extension AccountPortfoliosClient: DependencyKey {
request: { try await AccountPortfoliosClient.fetchAccountPortfolio(accountAddress) }
)

await state.setAccountPortfolio(portfolio)
await state.setOrUpdateAccountPortfolio(portfolio)

return portfolio
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ extension AccountList {
public func reduce(into state: inout State, viewAction: ViewAction) -> Effect<Action> {
switch viewAction {
case .task:

let accountAddress = state.account.address
state.portfolio = .loading
if state.portfolio.wrappedValue == nil {
state.portfolio = .loading
}

checkIfCallActionIsNeeded(state: &state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ public struct DevAccountPreferences: Sendable, FeatureReducer {

case let .callDone(controlStateKeyPath, changeTo):
if controlStateKeyPath == \State.faucetButtonState {
// NB: This call to update might be superfluous, since after any transaction we fetch all accounts
return updateAccountPortfolio(state).concatenate(with: loadIsAllowedToUseFaucet(&state))
} else {
state[keyPath: controlStateKeyPath] = changeTo
Expand Down
4 changes: 4 additions & 0 deletions Sources/Features/HomeFeature/Coordinator/Home.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public struct Home: Sendable, FeatureReducer {
public func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case let .accountsLoadedResult(.success(accounts)):
guard accounts.elements != state.accounts.elements else {
return .none
}

state.accountList = .init(accounts: accounts)
let accountAddresses = state.accounts.map(\.address)
return .run { _ in
Expand Down
Loading