前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >rxjs里scan和mergeScan operators的用法

rxjs里scan和mergeScan operators的用法

作者头像
Jerry Wang
发布2020-10-27 15:06:57
6890
发布2020-10-27 15:06:57
举报

mergeScan

Applies an accumulator function over the source Observable where the accumulator function itself returns an Observable, then each intermediate Observable returned is merged into the output Observable.

It’s like scan, but the Observables returned by the accumulator are merged into the outer Observable.

看个区别。

先用scan:

const click$ = fromEvent(document, 'click');
    const one$ = click$.pipe(mapTo(1));
    const seed = 0;
    const count$ = one$.pipe(
    scan((acc, one) => (acc + one), seed),

每次点击ui,会显示当前总的点击次数。

用mergeScan的实现:

const click$ = fromEvent(document, 'click');
const one$ = click$.pipe(mapTo(1));
const seed = 0;
const count$ = one$.pipe(
  mergeScan((acc, one) => of(acc + one), seed),
);
count$.subscribe(x => console.log(x));

唯一的区别就在于下图高亮之处:返回的是累加的Observable而不是原始值:

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • mergeScan
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档