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;
});
};