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