문제

I wish to get the same behaviour that you would get from a BehaviorSubject from an IObservable.

When I do a .Take(1) I wish for it to return the "default value" until the underlying IObservable changes it. The underlying IObservable "pumps" once and completes.

I do not wish to actually use a BehaviorSubject (it would make this trivial) as I believe this is bad form.

Is this possible?

도움이 되었습니까?

해결책

If you want a stream that will always have an initial value you might want to use .StartWith() method.

Under the hood this will do something very similar to Lee's answer except without the .TakeUntil().

You might need to combine this with .Replay(1) if you want your subscribers to receive the last value immediately.

다른 팁

public static IObservable<T> Behaviour<T>(IObservable<T> source)
{
    return Observable.Return(default(T)).TakeUntil(source).Concat(source);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top