有两个永远不会完成的流:
--a---b-c-d---e--f---->
-----1-------2------3-->
我想从第一个流获取在第二个流的最后一个事件之后发生的事件。
一开始它看起来像这样:
--a->
(?)
---->
(=)
--a->
第二个流发出第一个事件后,它看起来像这样:
--a---b-c-d->
(?)
-----1------>
(=)
------b-c-d->
第二个流中发生新事件后:
--a---b-c-d---e--f->
(?)
-----1-------2----->
(=)
--------------e--f->
依此类推...需要哪组运算符来执行此操作?
最佳答案
您可以使用CombineLatest根据需要返回事件,例如:
/* Have staggering intervals */
var source1 = Rx.Observable.interval(1000)
.map(function(i) {
return {
data: (i + 1),
time: new Date()
};
});
var source2 = Rx.Observable.interval(3000).startWith(-1)
.map(function(i) {
return {
data: (i + 1),
time: new Date()
};
});
// Combine latest of source1 and source2 whenever either gives a value with a selector
var source = Rx.Observable.combineLatest(
source1,
source2,
function(s1, s2) {
if (s1.time > s2.time) {
return s1.data + ', ' + s2.data;
}
return undefined;
}
).filter(x => x !== undefined).take(10);
var subscription = source.subscribe(
function(x) {
console.log('Next: %s', x);
},
function(err) {
console.log('Error: %s', err);
},
function() {
console.log('Completed');
});
关于javascript - 如何从一个流获取在其他流的最后一个事件之后发生的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38611336/