Concept
This is an angular2 supercharged project.
When you consume an observable stream from the redux store, I tried to filter first and then take / takeLast / last the most recent value. After that, I want to resolve the promise when the thread ends, but it does not work when using the takeLast operator.
So the question is: What operator setting can I use to get the last value from the stream ?
Customization
I have simplified the configuration of Angular 2 in this sense of using RxJs.
- The original observable is managed by the redux library and is not complete. Service
- provides some logic for getting the last value from a component stream
- uses a style of promise of value.
Here is a working example: https://fiddle.jshell.net/markus_falk/an41z6g9/
Reduction store mock:
var latestTime$ = new Rx.Subject();
setInterval(function(){
latestTime$.onNext(Date.now());
}, 2000);
Service Injection Layout:
var timeStore = null;
var getLatestTime = function() {
return new Promise((resolve, reject) => {
latestTime$
.filter(function(x) {
console.log('filter: ', x);
return x === typeof('number');
})
.takeLast(1)
.subscribe(
function (x) {
console.log('Next: ' + x);
timeStore = x;
},
function (err) {
console.log('Error: ' + err);
reject(err)
},
function () {
console.log('Completed');
resolve(timeStore);
}
);
});
};
And consuming component layout:
document.querySelector("#foo").addEventListener("click", function(event) {
var time = getLatestTime();
time.then((latestTime) => {
console.log('latestTime: ', latestTime);
});
time.catch((err) => {
console.log('oh oh: ', err);
});
}, false);
source
share