Add better tests, refactor a bit observableconcurrenthashset
MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/HistoryTabViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/HistoryTabViewModel.cs
@@ -36,7 +36,7 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
Transactions = new ObservableCollection<TransactionViewModel>();
RewriteTable();
- var coinsChanged = Observable.FromEventPattern(Global.WalletService.Coins, nameof(Global.WalletService.Coins.HashSetChanged));
+ var coinsChanged = Observable.FromEventPattern(Global.WalletService.Coins, nameof(Global.WalletService.Coins.CollectionChanged));
var newBlockProcessed = Observable.FromEventPattern(Global.WalletService, nameof(Global.WalletService.NewBlockProcessed));
var coinSpent = Observable.FromEventPattern(Global.WalletService, nameof(Global.WalletService.CoinSpentOrSpenderConfirmed));
@@ -207,9 +207,9 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
TransactionSortDirection = SortOrder.None;
}
RefreshOrdering();
-
}
}
+
public SortOrder AmountSortDirection
{
get => _amountSortDirection;
@@ -224,6 +224,7 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
RefreshOrdering();
}
}
+
public SortOrder TransactionSortDirection
{
get => _transactionSortDirection;
@@ -238,7 +239,8 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
RefreshOrdering();
}
}
- void RefreshOrdering()
+
+ private void RefreshOrdering()
{
if (TransactionSortDirection != SortOrder.None)
{

MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/ReceiveTabViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/ReceiveTabViewModel.cs
@@ -32,7 +32,7 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
_addresses = new ObservableCollection<AddressViewModel>();
Label = "";
- Observable.FromEventPattern(Global.WalletService.Coins, nameof(Global.WalletService.Coins.HashSetChanged))
+ Observable.FromEventPattern(Global.WalletService.Coins, nameof(Global.WalletService.Coins.CollectionChanged))
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(o =>
{

MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/WalletViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/WalletViewModel.cs
@@ -27,7 +27,7 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
public WalletViewModel(string name, bool receiveDominant)
: base(name)
{
- var coinsChanged = Observable.FromEventPattern(Global.WalletService.Coins, nameof(Global.WalletService.Coins.HashSetChanged));
+ var coinsChanged = Observable.FromEventPattern(Global.WalletService.Coins, nameof(Global.WalletService.Coins.CollectionChanged));
var coinSpent = Observable.FromEventPattern(Global.WalletService, nameof(Global.WalletService.CoinSpentOrSpenderConfirmed));
coinsChanged

MODIFY
WalletWasabi.Tests/ModelTests.cs
WalletWasabi.Tests/ModelTests.cs
@@ -187,82 +187,96 @@ namespace WalletWasabi.Tests
{
var set = new ObservableConcurrentHashSet<int>();
- set.HashSetChanged += Set_HashSetChanged;
+ set.CollectionChanged += Set_CollectionChanged;
try
{
- // HashSetChanged fire 1
+ // CollectionChanged fire 1
set.TryAdd(1);
Assert.Contains(1, set);
Assert.Single(set);
- // HashSetChanged don't fire
+ // CollectionChanged don't fire
set.TryAdd(1);
Assert.Contains(1, set);
Assert.Single(set);
- // HashSetChanged don't fire
+ // CollectionChanged don't fire
set.TryRemove(2);
Assert.Single(set);
- // HashSetChanged fire 2
+ // CollectionChanged fire 2
set.TryAdd(2);
Assert.Contains(2, set);
Assert.Equal(2, set.Count);
- // HashSetChanged fire 3
+ // CollectionChanged fire 3
set.TryRemove(2);
Assert.Contains(1, set);
Assert.DoesNotContain(2, set);
Assert.Single(set);
- // HashSetChanged fire 4
+ // CollectionChanged fire 4
set.TryAdd(3);
Assert.Contains(1, set);
Assert.Contains(3, set);
Assert.Equal(2, set.Count);
- // HashSetChanged fire 5
+ // CollectionChanged fire 5
set.Clear();
Assert.NotNull(set);
Assert.Empty(set);
}
finally
{
- set.HashSetChanged -= Set_HashSetChanged;
+ set.CollectionChanged -= Set_CollectionChanged;
}
}
- private long _set_HashSetChanged_InvokeCount = 0;
+ private long _set_CollectionChanged_InvokeCount = 0;
- private void Set_HashSetChanged(object sender, EventArgs e)
+ private void Set_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
- Interlocked.Increment(ref _set_HashSetChanged_InvokeCount);
+ Interlocked.Increment(ref _set_CollectionChanged_InvokeCount);
- switch (Interlocked.Read(ref _set_HashSetChanged_InvokeCount))
+ switch (Interlocked.Read(ref _set_CollectionChanged_InvokeCount))
{
case 1:
{
- Assert.Equal(EventArgs.Empty, e);
+ Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
+ Assert.Single(e.NewItems);
+ Assert.Null(e.OldItems);
+ Assert.Equal(1, e.NewItems[0]);
break;
}
case 2:
{
- Assert.Equal(EventArgs.Empty, e);
+ Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
+ Assert.Single(e.NewItems);
+ Assert.Null(e.OldItems);
+ Assert.Equal(2, e.NewItems[0]);
break;
}
case 3:
{
- Assert.Equal(EventArgs.Empty, e);
+ Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
+ Assert.Null(e.NewItems);
+ Assert.Single(e.OldItems);
+ Assert.Equal(2, e.OldItems[0]);
break;
}
case 4:
{
- Assert.Equal(EventArgs.Empty, e);
+ Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
+ Assert.Single(e.NewItems);
+ Assert.Null(e.OldItems);
+ Assert.Equal(3, e.NewItems[0]);
break;
}
case 5:
{
- Assert.Equal(EventArgs.Empty, e);
+ Assert.Equal(NotifyCollectionChangedAction.Reset, e.Action);
+ Assert.Null(e.NewItems);
+ Assert.Null(e.OldItems); // "Reset action must be initialized with no changed items."
break;
}
default: throw new NotSupportedException();

MODIFY
WalletWasabi/Models/ObservableConcurrentHashSet.cs
WalletWasabi/Models/ObservableConcurrentHashSet.cs
@@ -12,8 +12,6 @@ namespace WalletWasabi.Models
private ConcurrentHashSet<T> Set { get; }
private object Lock { get; }
- public event EventHandler HashSetChanged;
-
public event NotifyCollectionChangedEventHandler CollectionChanged;
public ObservableConcurrentHashSet()
@@ -42,8 +40,7 @@ namespace WalletWasabi.Models
}
if (invoke)
{
- HashSetChanged?.Invoke(this, EventArgs.Empty);
- CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new[] { item }));
+ CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
return invoke;
}
@@ -60,8 +57,7 @@ namespace WalletWasabi.Models
}
if (invoke)
{
- HashSetChanged?.Invoke(this, EventArgs.Empty);
- CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, new[] { item }));
+ CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
}
return invoke;
}
@@ -79,7 +75,7 @@ namespace WalletWasabi.Models
}
if (invoke)
{
- HashSetChanged?.Invoke(this, EventArgs.Empty);
+ // "Reset action must be initialized with no changed items."
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}

Add better tests, refactor a bit observableconcurrenthashset