Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

public static IObservable<T> Behaviour<T>(IObservable<T> source)
{
    return Observable.Return(default(T)).TakeUntil(source).Concat(source);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top