Open sericaer opened 5 years ago
tvalue=listUsed.ObserveRemove().ToReactiveProperty(); tvalue=listUsed.ObserveEveryValueChanged( => .Count).ToReactiveProperty();
@guopenglun That won't detect additions, only removals
I've done this as RxSum You need to observe add, remove, clear, replace and have those subscribeWithStwte using the output total reactive property.
The only issue I have with this is that the output property needs to be disposable and needs to dispose it's 4 subscriptions.
I also made the more generic RxAggregate; would be nice to just override the linq methods directly, but I'm afraid of leaking Disposables.
Thoughts on that would be appreciated.
Was at the beach when I wrote that... Here's some code. DisposableReadOnlyReactiveProperty is basically a reactive property that carries a disposable that will kill all those subscriptions; Would be happy to see a better alternative.
Also, it seems that SubscribeWithState, passing in aggregated would be better.
public static IDisposableReadOnlyReactiveProperty
public static IDisposableReadOnlyReactiveProperty<long> RxSum(this IReadOnlyReactiveCollection<long> source) => source.RxAggregate(0L, (a, b) => a + b, (a, b) => a - b);
public static IDisposableReadOnlyReactiveProperty<int> RxSum(this IReadOnlyReactiveCollection<int> source) => source.RxAggregate(0, (a, b) => a + b, (a, b) => a - b);
public static IDisposableReadOnlyReactiveProperty<float> RxSum(this IReadOnlyReactiveCollection<float> source) => source.RxAggregate(0f, (a, b) => a + b, (a, b) => a - b);
public static IDisposableReadOnlyReactiveProperty<double> RxSum(this IReadOnlyReactiveCollection<double> source) => source.RxAggregate(0d, (a, b) => a + b, (a, b) => a - b);
for example:
ReactiveProperty total is the sum of tvalue in UsedElem listUsed, listUsed can be insert and remove, and tvalue can also changed.
How to create ReactiveProperty total ?