[RxJS] Handling Multiple Streams with Merge

时间:2022-09-14 22:09:50

You often need to handle multiple user interactions set to different streams. This lesson shows hows Observable.merge behaves like a "logical OR" to have your stream handle one interaction OR another.

After click the start buttion, we wnat the logic that, click stop, it remember the current state, then if click start, continue with the current state.

If click reset, then it restart from 0;

const Observable = Rx.Observable;

const startButton = document.querySelector("#start");
const stopButton = document.querySelector("#stop");
const resetButton = document.querySelector("#reset"); const data = {count: 0};
const inc = (acc) => ({count: acc.count + 1});
const reset = (acc) => data; const start$ = Observable.fromEvent(startButton, 'click');
const stop$ = Observable.fromEvent(stopButton, 'click');
const reset$ = Observable.fromEvent(resetButton, 'click');
const interval$ = Observable.interval(500);
const intervalThatStop$ = interval$.takeUntil(stop$);
const incOrReset$ = Observable.merge(
intervalThatStop$.mapTo(inc),
reset$.mapTo(reset)
); start$
.switchMapTo(incOrReset$)
.startWith(data)
.scan( (acc, curr) => curr(acc))
.subscribe( (x) => console.log(x))

[RxJS] Handling Multiple Streams with Merge的更多相关文章

  1. [Recompose] Merge RxJS Button Event Streams to Build a React Counter Component

    Combining input streams then using scan to track the results is a common scenario when coding with s ...

  2. [RxJS] Refactoring Composable Streams in RxJS, switchMap()

    Refactoring streams in RxJS is mostly moving pieces of smaller streams around. This lessons demonstr ...

  3. [RxJS] Handling a Complete Stream with Reduce

    When a stream has completed, you often need to evaluate everything that has happened while the strea ...

  4. [RxJS] Use RxJS mergeMap to map and merge high order observables

    Like RxJS switchMap() is a shortcut for map() and switch(), we will see in this lesson how mergeMap( ...

  5. [Vue-rx] Share RxJS Streams to Avoid Multiple Requests in Vue.js

    Splitting a stream into multiple streams causes new subscriptions. You can think of new subscription ...

  6. [Angular 2] Mapping Streams to Values to Affect State

    While you have multiple streams flowing into your scan operator, you'll need to map each stream to t ...

  7. Visualize real-time data streams with Gnuplot

    源文地址 (September 2008) For the last couple of years, I've been working on European Space Agency (ESA) ...

  8. [转]Multiple outputs from T4 made easy

    本文转自:http://damieng.com/blog/2009/01/22/multiple-outputs-from-t4-made-easy One of the things I wante ...

  9. angular2 学习笔记 ( rxjs 流 )

    RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅,  ...

随机推荐

  1. AJAX跨域调用相关知识-CORS和JSONP(引)

    AJAX跨域调用相关知识-CORS和JSONP 1.什么是跨域 跨域问题产生的原因,是由于浏览器的安全机制,JS只能访问与所在页面同一个域(相同协议.域名.端口)的内容. 但是我们项目开发过程中,经常 ...

  2. 使用git对unity3d项目进行版本控制

    http://*.com/questions/18225126/how-to-use-git-for-unity-source-control The following is ...

  3. PowerShell管理IIS(新建站点、应用程序池、应用程序、虚拟目录等)

    #导入IIS管理模块 Import-Module WebAdministration #新建应用程序池 api.dd.com New-Item iis:\AppPools\api.dd.com Set ...

  4. 使用android x86进行android应用开发.

    首发论坛 安卓巴士.id:android_bin 一.必备工具: 1.虚拟机软件(本文使用virtual Box为例), 2.android x86镜像,下载地址http://www.android- ...

  5. Centos 升级MySQL版本或者Yum安装Mysql5.6

    Centos 升级MySQL版本或者Yum安装Mysql5.6 1.从MySQL Yum仓库下载最新的rpm文件:http://dev.mysql.com/downloads/repo/yum/Cen ...

  6. datatables表格

    datatables表格 并不是所有的后台开发都有美工和前端工程师来配合做页面,为了显示数据并有一定的美感,jQuery的DataTables插件对于像我这样的前端菜鸟来说真是雪中送炭,当然对于专业的 ...

  7. Java: 分解List<HashMap<String, String>>

    分解List<HashMap<String, String>> 的方法: List<HashMap<String, String>> mapList; ...

  8. jquery中attr和prop的区别分析

    这篇文章主要介绍了jquery中attr和prop的区别分析的相关资料,需要的朋友可以参考下 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别 ...

  9. scrapy实战之scrapyrt的使用

    scrapyrt为scrapy提供了一个http接口,有了它,我们不用再执行命令,而是直接请求一个http接口来启动项目,如果项目是部署在远程的,会比较方便. 1.安装: pip install sc ...

  10. SQL Server数据库存储过程中拼接字符串注意的问题

    在SQL Server数据库中书写复杂的存储过程时,一般的做法是拼接字符串,最后使用EXEC sp_executesql '拼接的字符串' 查询出结果. 先看一段代码: -- ============ ...