views:

40

answers:

2

I am trying to refine the filter in my Observer over time, and I was wondering what they best way of doing this is.

e.g. myObservable.Where(x=>x.Color=="red").Subscribe();

and then myObservable.Where(x=>x.Color=="blue").Subscribe();

and then merge the two into one stream so that OnNext() is called on Red OR Blue observables.

Maybe it hasn't fully clicked what is going on for me.

What if I also have myObservable.Where(x=>x.Type=="Car").Subscribe();. It will keep calling the same OnNext() method each time? What use is this to me.. I might want to react differently depending on which subscription calls the update, but at the same time I might want to flatten the subscriptions.

e.g. In the above scenario, if the colour is red I want to write 'new red object', and if it's a car I want to write 'new car'. How would I do this in Rx? There is an overload on the subscribe for OnNext,OnError etc.. but that requires the Observer to be an observable too (Subject).. correct me if I'm wrong.

This makes no sense to me.. why should something that is observing changes also be observable?

+1  A: 

How about this?

myObservable.Where(x=>x.Color=="red" || x.Color == "blue").Subscribe(x=>Console.WriteLine("new {0} object", x.Color));
myObservable.Where(x=>x.Type=="Car").Subscribe(x=>Console.WriteLine("new car"));

Jeffrey

Jeffrey van Gogh
if I have myObservable.Where(x=>x.Color=="red").Subscribe(x=>...);how do I then add x.Color=="blue"?Do I use .Merge()? Or..
sjhuk
A: 

Firstly, a Subject is an observable that generates in response to having observed other observables. They are useful as "plumbing" in some situations, and you can even use them to build a form of "agents" that communicate via channels.

For your main question: I think it may not have clicked because in your examples of Subscribe you've left out what Observer should be subscribed - it can't be ...Subscribe(), it has to be ...Subscribe(observer).

One answer to your question is that you can just subscribe the same observer to multiple observables. Or you could use Merge. If you want to be able to distinguish them, then the easiest way is to have two Subjects that observe, do what's specific to that kind of thing, then pass observations onwards to be Merged. Alternatively, you could use Select to tag observations prior to merging them.

RD1