Question

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.

Was it helpful?

Solution

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 => /* ... */);

OTHER TIPS

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(..);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top