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. cocos2d-x 3.0 画图节点——Node

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  2. Ubuntu网络管理

    1.重启NetworkManager service network-manager stop rm /var/lib/NetworkManager/NetworkManager.state serv ...

  3. Android Studio试用总结

    Android Studio是一年前Google I/O上推出的一款Android开发IDE,他基于JetBrains’ IntelliJ IDEA,目前还在preview阶段.增强了布局拖拽和预览功 ...

  4. 图形绘制 Canvas Paint Path 详解

    图形绘制简介        Android中使用图形处理引擎,2D部分是android SDK内部自己提供,3D部分是用Open GL ES 1.0.大部分2D使用的api都在android.grap ...

  5. 小学生之Log4j使用教程

    以前都是把所有日志都输出到一个文件下面,今天有个同事问想把某个包下的日志输出到 指定的地方,于是就在网上查了一些资料,总结一下,以免以后用到. 一.log4j是什么?  Log4j是一个开源的日志记录 ...

  6. javascript 识别移动端设备

    看到一种比较简单的方法,于是就把它记录下来备用吧.最近离职了,房子换了,还有...真是一把心酸,我知道谁活着都不容易,自己也资格把自己的苦水吐给别人,因为别人也过得不容易,所以大多不快都只能闷着,大家 ...

  7. 网页调用QQ聊天

    把下面的复制到地址栏里,QQ号为你要聊天的人的qq号,如果你没有登录你自己的qq,首先会调出qq登录窗体. tencent://message/?uin=QQ号­

  8. 小技巧之jQueryMobile

    使用JqueryMobile+MVC做一个手机网站,也有2个月了.有一些小小的经验,跟大伙们分享一下下 小技巧1: 禁用所有Ajax加载,它会很烦人的. $.mobile.ajaxLinksEnabl ...

  9. Kinect研究

    1. 深度照相机 红外 + 2个摄像头. (1彩色摄像头,1红外发射,1接收) 红外捕捉深度. 2. OpenNI 某编程社区创建. 提供基本深度数据的访问.LGPL协议. 用户跟踪付费. 3. 问题 ...

  10. Oracle Enterprise Manager快速重建

    我们在使用Oracle时, 可以利用Oracle自带的EM(Enterprise Manager)来更方便的管理我们的数据库.但是有时候我们的em却有时候无法连接,造成这个问题的原因有好多,例如没有正 ...