In my Avalonia app, I have lists of items, each with a weight property. The user is able to influence these values through the UI. I would like to produce a total weight whenever an item changes, which appears to be the central theme around Dynamic Data.
I’m new to FRP, and even newer to Dynamic Data. I’ve mostly been experimenting with these the past week or two and I’ve just discovered the ForAggregation()
operator, which appeared to speed things up noticeably, but never seems to produce the right sum. In fact, the sum is correct the first time, but never again.
Here’s a minimal example:
View Model:
public partial class ItemViewModel : ViewModelBase
{
[ObservableProperty]
private int _weight;
}
Main window:
public partial class MainViewModel : ViewModelBase, IDisposable
{
private readonly SourceList<ItemViewModel> _cache;
private readonly ItemViewModel _thing;
public MainViewModel()
{
_thing = new() { Weight = 1 };
_cache = new();
_cache.Add(_thing); // add just 1 item
_cache.Connect()
.AutoRefresh(item => item.Weight)
.ForAggregation()
.Sum(item => item.Weight)
.Subscribe(sum => Debug.WriteLine($"The sum is: {sum}"));
}
public void Clicked()
{
Random r = new();
_thing.Weight = r.Next(1, 10);
}
}
So we have a SourceCache
of all the items, which is just 1 in this example, with its weight being just 1.
Each time I click my button, the weight changes. AutoRefresh()
detects the property change and forces the remainder of the pipeline to recompute the total weighting. Each time, however, I get “The sum is: 1”, even though I’m printing out a different weight on the only item in my list.
My guess is that when the Aggregator tracks changes, it does something like
Change.Previous = oldItem;
Change.Current = newItem;
sumDiff = newItem.Weight - oldItem.Weight;
Which makes things fast and efficient. But since my class is a reference type, oldItem
and newItem
are the same object, and so sumDiff
is 0, therefore doing nothing to the sum…
I was using ToCollection()
and regular LINQ Sum()
before, and it worked, but was noticeably slower. I do have another idea in mind I’ll experiment with later today, but for now, I just want to know:
- whether my guess was correct
- is
ForAggregation()
the right operator for what I want to do?