I have a ReactiveUI-like view model. It has several properties of different types which fire NotifyPropertyChanged
events and I want to subscribe a method that will be called when any is fired, but I am not interested in actual values.
My current code is a bit ugly (due to opaque true
selects). Is there a way to express this which shows the intention of just caring when the event occurs?
this.ObservableForProperty(m => m.PropertyOne)
.Select(_ => true)
.Merge(this.ObservableForProperty(m => m.PropertyTwo).Select(_ => true))
.Subscribe(...)
I am merging about 8 properties, so it's more ugly than shown.
Since this looks like ReactiveUI, how about using the WhenAny operator:
this.WhenAny(x => x.PropertyOne, x => x.PropertyTwo, (p1, p2) => Unit.Default)
.Subscribe(x => /* ... */);
In general though, if you were combining arbitrary Observables, you could also write this a bit more clearly using the non-extension method:
Observable.Merge(
this.ObservableForProperty(x => x.PropertyOne).Select(_ => Unit.Default),
this.ObservableForProperty(x => x.PropertyTwo).Select(_ => Unit.Default),
this.ObservableForProperty(x => x.PropertyThree).Select(_ => Unit.Default)
).Subscribe(x => /* ... */);
Also, if you're subscribing to every property of a ReactiveObject, it's probably better to just use:
this.Changed.Subscribe(x => /* ... */);
You could make it an extension method to make the intent clearer:
public static IObservable<bool> IgnoreValue<T>(this IObservable<T> source)
{
return source.Select(_ => true);
}
...
this.ObservableForProperty(m => m.PropertyOne).IgnoreValue()
.Merge(this.ObservableForProperty(m => m.PropertyTwo).IgnoreValue())
.Subscribe(..);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With