Rxjs 5中的CombineLatest行为? [英] combineLatest behaviour in Rxjs 5?

查看:202
本文介绍了Rxjs 5中的CombineLatest行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 定义 (combineLatest

组合多个Observable创建一个Observable,其值是 根据其每个输入Observable的最新值进行计算.

Combines multiple Observables to create an Observable whose values are calculated from the latest values of each of its input Observables.

只要任何输入Observable发出一个值,它就会计算一个公式 使用所有输入的最新值,然后发出输出 该公式.

Whenever any input Observable emits a value, it computes a formula using the latest values from all the inputs, then emits the output of that formula.

很明显,在每个时间段/排放中,该排放的结果是最新排放的组合.

It's pretty clear to see that at each period of time / emission , the result at that emission is the combination of the latest emissions.

如果是,请查看此代码

const obs1 = Rx.Observable.of(1, 2, 3, 4, 5);
const obs2 = Rx.Observable.of('a', 'b', 'c')
const obs3 = obs2.combineLatest(obs1, (a, b) => a+b);

const subscribe = obs3.subscribe(latestValues => {
  console.log(latestValues   );
});

结果是:

c1,c2,c3,c4,c5

当我改变

obs2.combineLatest(obs1..

obs1.combineLatest(obs2..

-我得到5a,5b,5c

问题:

文档没有在调用顺序上指定任何区别(如果这样,为什么我会得到不同的结果)?

The docs don't specify any difference regarding the order of invocation (if so - why do I get different results) ?

为什么我看不到与其他来源的其他组合?似乎一个来源正在获取其最后一个值,而只有这样-将其与其他来源中的每个值连接起来.

Why don't I see other combinations with the other source ? It seems that one source is taking its last value and only then - join it to each value from the OTHER source.

这似乎是实际发生的情况:

It seems like that this is what actually happening:

[other source]---a---b---c------------------------
[first source]-------------1----2-----3-----4----5
[result]-------------------c1---c2----c3----c4---c5

当我交换订单(obs1<-> obs2)时:

And when I swap the order (obs1<->obs2) :

[first source]-----1--2---3--4--5----------------
[other source]---------------------a-----b------c
[result]---------------------------5a----5b-----5c

这是怎么回事?为什么必须要完成一个流才能使联接开始?

What's going on here? why does one stream has to finish in order for the join to start ?

为什么我看不到这样的东西(或变体):

Why don't I see something like this ( or a variation) :

[first source]-----1-----2--------3-------4------5---
[other source]---------a-----b------c----------------
[result]---------------1a------2b------3b---4c-------5c

JSBIN

推荐答案

这是因为,除非您使用调度程序,否则同步地发出源Observable的值.这意味着第一个obs1会发出所有值,而combineLatest只会记住最后一个,而obs2会开始发出其所有值(如果切换两个Observable,则表示相同).现在combineLatest具有两个可观测值的值,因此第二个可观测值的任何发射都将使运算符发射一个值.

This is because values from your source Observables are emited synchronously unless you use a Scheduler. This means that first obs1 emits all values and only the last one is remembered by combineLatest and than obs2 starts emiting all its values (the same appplies if you switch the two Observables). Now the combineLatest has values for both Observables so any emission from the second Observables will make the operator emit a value.

为什么这样工作是因为Observable.of在内部被实现为ArrayObservable,并且默认情况下它不使用任何调度程序.这意味着其所有排放都是同步发生的.参见 https://github.com/ReactiveX/rxjs/blob/master/src/observable/ArrayObservable.ts#L118

Why it works like this is because Observable.of is implemented as ArrayObservable internally and by default it doesn't use any Scheduler. This means that all its emissions happen synchronously. See https://github.com/ReactiveX/rxjs/blob/master/src/observable/ArrayObservable.ts#L118

顺便说一句,您可以将Rx.Scheduler.async作为最后一个参数添加到两个源Observable中.

Btw, you can add Rx.Scheduler.async as the last parameter to both source Observables.

这篇关于Rxjs 5中的CombineLatest行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆