CombineLatest 运算符的行为类似于 Zip,但 Zip 仅在每个压缩的源 Observable 都发出一个之前未压缩的项目时才会发出项目,而 CombineLatest 则是在任何源 Observable 发出一个项目时就会发出项目(只要每个源 Observable 至少发出过一个项目)。当任何一个源 Observable 发出一个项目时,CombineLatest 会使用您提供的函数组合来自每个其他源 Observable 的最新发出的项目,并发出该函数的返回值。
combineLatestwithLatestFrom待定
RxGroovy 将此运算符实现为 combineLatest。它可以接受两个到九个 Observable(以及组合函数)作为参数,或者一个包含 Observable 的 List(以及组合函数)。它默认情况下不会在任何特定 Scheduler 上运行。
combineLatest(List,FuncN)combineLatest(Observable,Observable,Func2) (还有接受最多九个 Observable 的版本)
正在开发中,但不是 1.0 版本的一部分,是 withLatestFrom 运算符。它类似于 combineLatest,但只有在单个源 Observable 发出一个项目时才会发出项目(而不是像 combineLatest 那样,在传递给运算符的任何 Observable 发出项目时才会发出项目)。
RxJava 将此运算符实现为 combineLatest。它可以接受两个到九个 Observable(以及组合函数)作为参数,或者一个包含 Observable 的 List(以及组合函数)。它默认情况下不会在任何特定 Scheduler 上运行。
combineLatest(List,FuncN)combineLatest(Observable,Observable,Func2) (还有接受最多九个 Observable 的版本)
正在开发中,但不是 1.0 版本的一部分,是 withLatestFrom 运算符。它类似于 combineLatest,但只有在单个源 Observable 发出一个项目时才会发出项目(而不是像 combineLatest 那样,在传递给运算符的任何 Observable 发出项目时才会发出项目)。
RxJS 将此运算符实现为 combineLatest。它可以接受可变数量的单个 Observable(以及组合函数)作为参数,或者一个包含 Observable 的 Array(以及组合函数)。
/* Have staggering intervals */
var source1 = Rx.Observable.interval(100)
.map(function (i) { return 'First: ' + i; });
var source2 = Rx.Observable.interval(150)
.map(function (i) { return 'Second: ' + i; });
// Combine latest of source1 and source2 whenever either gives a value
var source = source1.combineLatest(
source2,
function (s1, s2) { return s1 + ', ' + s2; }
).take(4);
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x.toString());
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});Next: First: 0, Second: 0 Next: First: 1, Second: 0 Next: First: 1, Second: 1 Next: First: 2, Second: 1 Completed
RxJS 还拥有一个 withLatestFrom 运算符。它类似于 combineLatest,但只有在单个源 Observable 发出一个项目时才会发出项目(而不是像 combineLatest 那样,在传递给运算符的任何 Observable 发出项目时才会发出项目)。
/* Have staggering intervals */
var source1 = Rx.Observable.interval(140)
.map(function (i) { return 'First: ' + i; });
var source2 = Rx.Observable.interval(50)
.map(function (i) { return 'Second: ' + i; });
// When source1 emits a value, combine it with the latest emission from source2.
var source = source1.withLatestFrom(
source2,
function (s1, s2) { return s1 + ', ' + s2; }
).take(4);
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x.toString());
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});Next: First: 0, Second: 1 Next: First: 1, Second: 4 Next: First: 2, Second: 7 Next: First: 3, Second: 10 Completed
这两个运算符在以下每个发行版中都可用
rx.jsrx.compat.jsrx.lite.jsrx.lite.compat.jsRxPHP 将此运算符实现为 combineLatest。
使用选择器函数将指定的 Observable 序列合并为一个 Observable 序列,只要任何一个 Observable 序列生成一个元素就会调用该函数。Observable 必须是一个数组。如果省略结果选择器,则将产生包含元素的列表。
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/combineLatest/combineLatest.php
/* Have staggering intervals */
$source1 = \Rx\Observable::interval(100);
$source2 = \Rx\Observable::interval(120);
$source = $source1->combineLatest([$source2], function ($value1, $value2) {
return "First: {$value1}, Second: {$value2}";
})->take(4);
$subscription = $source->subscribe($stdoutObserver);
Next value: First: 0, Second: 0
Next value: First: 1, Second: 0
Next value: First: 1, Second: 1
Next value: First: 2, Second: 1
Complete!
RxPHP 还拥有一个 withLatestFrom 运算符。
使用选择器函数将指定的 Observable 序列合并为一个 Observable 序列,只有在(第一个)源 Observable 序列生成一个元素时才会调用该函数。
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/withLatestFrom/withLatestFrom.php
/* Have staggering intervals */
$source1 = \Rx\Observable::interval(140)
->map(function ($i) {
return 'First: ' . $i;
});
$source2 = \Rx\Observable::interval(50)
->map(function ($i) {
return 'Second: ' . $i;
});
$source3 = \Rx\Observable::interval(100)
->map(function ($i) {
return 'Third: ' . $i;
});
$source = $source1->withLatestFrom([$source2, $source3], function ($value1, $value2, $value3) {
return $value1 . ', ' . $value2 . ', ' . $value3;
})->take(4);
$source->subscribe($stdoutObserver);
Next value: First: 0, Second: 1, Third: 0
Next value: First: 1, Second: 4, Third: 1
Next value: First: 2, Second: 7, Third: 3
Next value: First: 3, Second: 10, Third: 4
Complete!