문제

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