Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I provide the latest value of a hot observable on subscribe

I have a hot observable (from an event) that I'm calling DistinctUntilChanged on which will have multiple subscribers who will subscribe at different times after the observable has started running and produced its first value. Subscribers will get getting the IObservable through a property on my class.

How do I make it so that each time someone subscribes to the observable they get the last value published but the observable acts normally otherwise? I think I might be looking for PublishLast but I'm not sure if it has other side effects.

Similar question: How do I get an IObservable to push the newest value upon subscription? This is a very similar question but it's from over a year ago and a lot of additions have been made to Rx so I think there might be a built-in function now rather than having to rely on a BehaviorSubject so I don't think this is an exact duplicate.

Edit: Here is what I'm actually trying to do. There's a comment below the actual observable sequence I'm talking about.

like image 711
Bryan Anderson Avatar asked Dec 10 '11 23:12

Bryan Anderson


People also ask

Which method is use to show only the final value emitted on source by Observable?

lastOrDefault also has a variant to which you can pass a predicate function, so that its Observable will emit the last item from the source Observable that the predicate evaluates as true , or the default item if no items emitted by the source Observable pass the predicate.

What does subscribing to an Observable do?

Subscribing to an Observable is like calling a function, providing callbacks where the data will be delivered to. This is drastically different to event handler APIs like addEventListener / removeEventListener . With observable.subscribe , the given Observer is not registered as a listener in the Observable.

What is the difference between Hot & Cold Observable?

To understand the concept of a hot and cold Observable it's good to always refer to what the data producer is. To make it simple: When the data is produced by the Observable itself, we call it a cold Observable. When the data is produced outside the Observable, we call it a hot Observable.

What is the type of Observable when the producer is created and activated during subscription?

An observable is “cold” if its underlying producer is created and activated during subscription. This means, that if observables are functions, then the producer is created and activated by calling that function. const socket = new WebSocket('ws://someurl'); socket.


1 Answers

The other answer was close. You either want a ReplaySubject(1) ie only replay 1 value; or you want a BehaviorSubject. The difference is a Behavior Subject will require a default value. This will also guarantee that the subscriber will always get a value immediately.

var replay1 = source.Replay(1);
replay1.Connect();
//Or
var alwaysHaveValue = source.Multicast(new BehaviorSubject<int>(-1));

To understand each of the Subjects check out my Intro to Rx post

like image 153
Lee Campbell Avatar answered Sep 25 '22 19:09

Lee Campbell