RxJS mergeScan

2020-10-13 17:48 更新

在源 Observable 上应用累加器功能 累加器函数本身返回一个 Observable,然后每个中间 返回的 Observable 合并到输出 Observable 中。

mergeScan<T, R>(accumulator: (acc: R, value: T, index: number) => any, seed: R, concurrent: number = Number.POSITIVE_INFINITY): OperatorFunction<T, R>

参量

累加器 在每个源值上调用累加器函数。
种子 初始累积值。
Simultaneously 可选的。 默认值为 Number.POSITIVE_INFINITY。  最大数量  输入并发预订的可观察对象。

returns

OperatorFunction<T, R>:可观察到的累积值。

描述

就像 scan,但是 Observables 返回了 由累加器合并到外部 Observable 中。

计算点击事件的数量

  1. import { fromEvent, of } from 'rxjs';
  2. import { mapTo, mergeScan } from 'rxjs/operators';
  3. const click$ = fromEvent(document, 'click');
  4. const one$ = click$.pipe(mapTo(1));
  5. const seed = 0;
  6. const count$ = one$.pipe(
  7. mergeScan((acc, one) => of(acc + one), seed),
  8. );
  9. count$.subscribe(x => console.log(x));
  10. // Results:
  11. // 1
  12. // 2
  13. // 3
  14. // 4
  15. // ...and so on for each click
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号