Question

I'm looking for the best way to implement cyclic IObservable chains.

For example:

IObservable<B> Foo(IObservable<A> fizz)
{
    //stuff
}

IObservable<A> Bar(IObservable<A> fizz, IObservable<B> buzz)
{
    //stuff
}

My implementation:

var fizzes = new BehaviorSubject<A>();
var buzzes = Foo(fizzes);
Bar(fizzes, buzzes).Subscribe(f => fizzes.OnNext(f));

Is there a better way to do this? Feels dirty using Subject, and generally less-than-elegant.

Was it helpful?

Solution

The line

Bar(fizzes, buzzes).Subscribe(f => fizzes.OnNext(f));

can be made simpler and changed to

Bar(fizzes, buzzes).Subscribe(fizzes);

as the Subscribe method takes an IObserver, the lambda methods that are normally used are just shortcuts for this.

As such, fizzes is being used as an IObserver and an IObservable, which is the definition of a Subject. If this is the behavior that you want, then a Subject is probably the way to go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top