Skip to content

Commit

Permalink
Call CreateCoinModels before GetPockets (WalletWasabi#13302)
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolay authored Aug 2, 2024
1 parent 301bbff commit bea1dbe
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
22 changes: 22 additions & 0 deletions WalletWasabi.Fluent/Extensions/ObservableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,26 @@ public static IObservableCache<TObject, TKey> Fetch<TObject, TKey>(this IObserva
.DisposeMany()
.AsObservableCache();
}

public static (IObservableCache<TObject1, TKey1>, IObservableCache<TObject2, TKey2>) Fetch<TObject1, TKey1, TObject2, TKey2>(
this IObservable<Unit> signal,
(Func<IEnumerable<TObject1>> Source, Func<TObject1, TKey1> KeySelector, IEqualityComparer<TObject1>? EqualityComparer) first,
(Func<IEnumerable<TObject2>> Source, Func<TObject2, TKey2> KeySelector, IEqualityComparer<TObject2>? EqualityComparer) second)
where TKey1 : notnull
where TKey2 : notnull
where TObject1 : notnull
where TObject2 : notnull
{
var cache1 = signal.Select(_ => first.Source())
.EditDiff(first.KeySelector, first.EqualityComparer)
.DisposeMany()
.AsObservableCache();

var cache2 = signal.Select(_ => second.Source())
.EditDiff(second.KeySelector, second.EqualityComparer)
.DisposeMany()
.AsObservableCache();

return (cache1, cache2);
}
}
15 changes: 11 additions & 4 deletions WalletWasabi.Fluent/Models/Wallets/CoinListModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public abstract partial class CoinListModel : IDisposable
{
private readonly CompositeDisposable _disposables = new();

private object Lock { get; } = new();

public CoinListModel(Wallet wallet, IWalletModel walletModel)
{
Wallet = wallet;
Expand All @@ -32,8 +34,13 @@ public CoinListModel(Wallet wallet, IWalletModel walletModel)
.Merge(isSelected)
.Publish();

Pockets = signals.Fetch(GetPockets, x => x.Labels).DisposeWith(_disposables);
List = signals.Fetch(() => Wallet.Coins.Select(CreateCoinModel), x => x.Key).DisposeWith(_disposables);
(List, Pockets) = signals.Fetch(
first: (Source: CreateCoinModels, KeySelector: x => x.Key, EqualityComparer: null),
second: (Source: GetPockets, KeySelector: x => x.Labels, EqualityComparer: null)
);

List.DisposeWith(_disposables);
Pockets.DisposeWith(_disposables);

signals
.Do(_ => Logger.LogDebug($"Refresh signal emitted in {walletModel.Name}"))
Expand All @@ -56,14 +63,14 @@ public ICoinModel GetCoinModel(SmartCoin smartCoin)
return List.Items.First(coinModel => coinModel.Key == smartCoin.Outpoint.GetHashCode());
}

private ICoinModel CreateCoinModel(SmartCoin smartCoin)
protected ICoinModel CreateCoinModel(SmartCoin smartCoin)
{
return new CoinModel(smartCoin, WalletModel.Settings.AnonScoreTarget);
}

protected abstract Pocket[] GetPockets();

protected abstract ICoinModel[] GetCoins();
protected abstract ICoinModel[] CreateCoinModels();

public void Dispose() => _disposables.Dispose();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace WalletWasabi.Fluent.Models.Wallets;
[AutoInterface]
public partial class UserSelectionCoinListModel(Wallet wallet, IWalletModel walletModel, SmartCoin[] selectedCoins) : CoinListModel(wallet, walletModel)
{
protected override ICoinModel[] GetCoins()
protected override ICoinModel[] CreateCoinModels()
{
return selectedCoins.Select(GetCoinModel).ToArray();
return selectedCoins.Select(CreateCoinModel).ToArray();
}

protected override Pocket[] GetPockets()
Expand Down
4 changes: 2 additions & 2 deletions WalletWasabi.Fluent/Models/Wallets/WalletCoinsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ protected override Pocket[] GetPockets()
return Wallet.GetPockets().ToArray();
}

protected override ICoinModel[] GetCoins()
protected override ICoinModel[] CreateCoinModels()
{
return Wallet.Coins.Select(GetCoinModel).ToArray();
return Wallet.Coins.Select(CreateCoinModel).ToArray();
}
}

0 comments on commit bea1dbe

Please sign in to comment.