Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive extension method to convert a hot observable interval to a cold observable

Lets say I have this hot observable that publishes consecutive numbers every second for 5 minutes:

1, 2, ... n, OnCompleted

At a certain point in time, after the hot observable has started, but before it has completed, I subscribe to it until it completes.

I get numbers: x, x+1, x+2, ... n.

I want to convert the values I received to a cold observable. Is there a special operator for this?

I know I could just use

Observable.Create(observer => hotObservable.Subscribe(onNext, onCompleted, onError);

But I'm sure there's an Rx extension method I'm missing, that does just that

like image 762
Liviu Trifoi Avatar asked Feb 22 '23 22:02

Liviu Trifoi


1 Answers

Just use Replay Subject.

ReplaySubject<int> sub = new ReplaySubject<int>();
hotObservable.Subscribe(sub);
//Now any one can subscribe to sub and it will get all items that hot observable sent to replay subject 
like image 66
Ankur Avatar answered Mar 09 '23 01:03

Ankur