Reputation: 10513
I've got three IObservable
of types Foo
, Bar
and Baz
. In addition, there is a method defined as:
void DoWork(Foo foo);
The IObservable
are defined elsewhere as Subject
and OnNext
is called from time to time.
Whenever new data is available (defined by some Where queries), I want to call the DoWork
method with the latest Foo
value. If no Foo
values were generated the method should not be called.
What is the easiest way to do that?
To be more specific, see the following example. I'd like to call DoWork
when bar
or baz
change, with the latest foo
(f.id == 1
):
void Wire(IObservable<Foo> foo, IObservable<Bar> bar, IObservable<Baz> baz)
{
foo.Where(f => f.Id == 1)
.Subscribe(f => DoWork(f));
}
Upvotes: 2
Views: 170
Reputation: 117027
This works for me:
var ubar = bar.Select(x => Unit.Default);
var ubaz = baz.Select(x => Unit.Default);
ubar.Merge(ubaz)
.CombineLatest(foo.Where(f => f.Id == 1), (u, f) => f)
.Subscribe(f => DoWork(f));
Upvotes: 1
Reputation: 74530
You can use the Do
extension method on the Observable
class to do this:
IObservable<Foo> filteredList = foos.Where(...);
// Invoke DoWork on each instance of Foo.
filteredList.Do(DoWork);
Upvotes: 0