Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more elegant way to merge observables when return type is unimportant?

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.

like image 946
Valentin Kuzub Avatar asked Nov 04 '11 14:11

Valentin Kuzub


2 Answers

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 => /* ... */);
like image 129
Ana Betts Avatar answered Nov 07 '22 13:11

Ana Betts


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(..);
like image 30
Thomas Levesque Avatar answered Nov 07 '22 15:11

Thomas Levesque