RxJS combineAll 运算符,解释 [英] RxJS combineAll Operator, explanation

查看:69
本文介绍了RxJS combineAll 运算符,解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 combineAll 运算符的工作原理.我正在使用

从文档中,我读到每次任何内部 Observable 发出一个值时,这个发出的值都会与所有其他内部 observable 的最后一个值组合.

在上面的方案中,我们可以看到内部 Observables 在一段时间内发出了 10 个值,所以我期望随着时间的推移得到 10 个值的输出,但结果是 9.

另外,在输出的第一行:

["Result (0): 0", "Result (1): 0"])

'Result (1): 0' 的 0 是否对应于空值?因为 Observable 'inner 2' 还没有发出任何东西?

在这里完成是我期望的输出:

["Result (0): 0", "Result (1): 0"][结果(0):1",结果(1):0"][结果(0):1",结果(1):0"][结果(0):2",结果(1):0"][结果(0):2",结果(1):1"][结果(0):3",结果(1):1"][结果(0):3",结果(1):2"][结果(0):4",结果(1):2"][结果(0):4",结果(1):3"][结果(0):4",结果(1):4"]

这显然是错误的,但我没有发现我的错误,有人可以解释一下吗?

解决方案

考虑到 combineAll:

<块引用>

在 Observable-of-Observables 完成时应用 combineLatest 将 Observable-of-Observables 展平.

还有 combineLatest;

<块引用>

实际上会等待所有输入 Observable 至少发出一次.

因此 combineAll observable 的第一次发射包括Inner 1" observable 的第一个值,直到Inner 2" observable 发射它的第一个值时才会发生.所以只会有九次排放,而不是十次.

I'm trying to understand how does the combineAll operator work. I'm using the following example from the official documentation:

import { take, map, combineAll } from 'rxjs/operators';
import { interval } from 'rxjs';

const source$ = interval(1000).pipe(take(2));

const example$ = source$.pipe(
  map(val =>
    interval(1000).pipe(
      map(i => `Result (${val}): ${i}`),
      take(5)
    )
  )
);

example$
  .pipe(combineAll())
  .subscribe(console.log);

The output is then :

["Result (0): 0", "Result (1): 0"]
["Result (0): 1", "Result (1): 0"]
["Result (0): 1", "Result (1): 1"]
["Result (0): 2", "Result (1): 1"]
["Result (0): 2", "Result (1): 2"]
["Result (0): 3", "Result (1): 2"]
["Result (0): 3", "Result (1): 3"]
["Result (0): 4", "Result (1): 3"]
["Result (0): 4", "Result (1): 4"]

Trying to figure why, I made this simple scheme:

From the documentation, I read that each time any of the inner Observable emits a value, then this emitted value is combined by the last value of all the other inner observables.

In the scheme above, we can see that 10 values are emitted during time by the inner Observables, so I was expecting to get an output with 10 values over the time, but it's 9.

Also, in the first line of the output :

["Result (0): 0", "Result (1): 0"]) 

Does the 0 of 'Result (1): 0' correspond to a null value? Because Observable 'inner 2' has not yet emitted anything?

To finish here is what I was expecting as output :

["Result (0): 0", "Result (1): 0"]
["Result (0): 1", "Result (1): 0"]
["Result (0): 1", "Result (1): 0"]
["Result (0): 2", "Result (1): 0"]
["Result (0): 2", "Result (1): 1"]
["Result (0): 3", "Result (1): 1"]
["Result (0): 3", "Result (1): 2"]
["Result (0): 4", "Result (1): 2"]
["Result (0): 4", "Result (1): 3"]
["Result (0): 4", "Result (1): 4"]

It's obviously wrong but I don't find my mistake, could someone explain?

解决方案

Consider that combineAll:

flattens an Observable-of-Observables by applying combineLatest when the Observable-of-Observables completes.

And that combineLatest;

will actually wait for all input Observables to emit at least once.

So the first emission from the combineAll observable that includes the first value of the "Inner 1" observable is not going to happen until the "Inner 2" observable emits its first value. So there will only be nine emissions - not ten.

这篇关于RxJS combineAll 运算符,解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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