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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top