Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish the last value of an observable

Tags:

I have a hot observable (a subject in this case):

var subject = new Rx.Subject(); 

I want to create another observable that every time a new subscriptions is being made immediately fires out the last value that was produced.

So in pseudo code:

var myObservableWithLastValue = subject.publishLast();  subject.onNext(3);  myObservableWithLastValue.subscribe(function(x){     console.log(x); //should write 3 });  myObservableWithLastValue.subscribe(function(x){     console.log(x); //should write 3, too });  subject.onNext(4);  myObservableWithLastValue.subscribe(function(x){     console.log(x); //should write 4 }); 

This is roughly what I want and it seems to work. However, I guess there must be some built in mechanism to achieve the same

Rx.Observable.prototype.keepLatest = function () {     var latestValue;      var disposable = this.subscribe(function (value) {         latestValue = value;     });      return Rx.Observable.create(function (observer) {         observer.onNext(latestValue);         return disposable.dispose;     }); }; 
like image 578
Christoph Avatar asked Mar 16 '12 10:03

Christoph


People also ask

How do you end an observable?

Use the unsubscribe method A Subscription essentially just has an unsubscribe() function to release resources or cancel Observable executions. To prevent this memory leaks we have to unsubscribe from the subscriptions when we are done with. We do so by calling the unsubscribe method in the Observable.

How do I get last emitted value from BehaviorSubject?

The BehaviorSubject There are two ways to get this last emited value. You can either get the value by accessing the . value property on the BehaviorSubject or you can subscribe to it. If you subscribe to it, the BehaviorSubject will directly emit the current value to the subscriber.

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

The Filtering Operators You can also pass a predicate function to last , in which case it will produce an Observable that emits only the last item from the source Observable that the predicate evaluates as true .

How to subscribe observable?

Subscribinglink An Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the subscribe() method of the instance, passing an observer object to receive the notifications. Returns an Observable instance that synchronously delivers the values provided as arguments.


2 Answers

RxJs now has the ReplaySubject. Initialize it with 1 buffer and you have the BehaviorSubject.

// as an example, use buffer size of 2  var subject = new Rx.ReplaySubject(2 /* buffer size */);    subject.onNext('a');  subject.onNext('b');  subject.onNext('c');    subject.subscribe(function (x) { document.write('x1:' + x + '<br>'); });    subject.onNext('d');    subject.subscribe(function (x) { document.write('x2:' + x + '<br>'); });
<script src='https://rawgit.com/Reactive-Extensions/RxJS/v.2.5.3/dist/rx.all.js'></script>
like image 96
allprog Avatar answered Sep 27 '22 19:09

allprog


BehaviorSubject:

Initializes a new instance of the Rx.BehaviorSubject class which creates a subject that caches its last value and starts with the specified value.

var subject = new Rx.BehaviorSubject('a' /* initial value */);    subject.subscribe(function (x) {      console.log('x1:' + x);  });    subject.onNext('d');    // Will produce the last value.    subject.subscribe(function (x) {      console.log('x2:' + x);  });
<script src='https://rawgit.com/Reactive-Extensions/RxJS/v.2.5.3/dist/rx.all.js'></script>
like image 29
Asti Avatar answered Sep 27 '22 19:09

Asti