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. Java static块

    首先,我们看一个实际例子: class Test{ public static int X=100; public final static int Y=200; public Test(){ Sys ...

  2. 解决svn状态图标不显示的办法

    SVN是一款出色的代码版本控制工具,大部分开发者都在使用.由于前不久刚做了系统,所以要重装一下SVN,结果就出了问题,问题就是,不管是文件处于什么状态他的提示图标都不显示,这就太不给力了吧.通过搜寻, ...

  3. [编译原理代码][NFA转DFA并最小化DFA并使用DFA进行词法分析]

    #include <iostream> #include <vector> #include <cstring> #include "stack" ...

  4. mybatis 与 反射

    Mybatis是个优秀的ORM框架,所以它的反射层一定不会让我们失望 图比较大,可以开新页面查看 可以看到,Mybatis对这一块抽象的比较复杂,我们可以看到有几个比较主要的部分:Reflector. ...

  5. CSS 特殊样式设置集合

    1. 父窗口宽度不定,要求内部两个子块, 第一个子块宽度固定,第二个子块宽度自适应. 第一个子块宽度固定,定位为绝对定位 position:absolute;  第二个子块设置margin-left即 ...

  6. python challenge 待续中

    网址:http://www.pythonchallenge.com/解答好文:http://story.iteye.com/blog/730466 0:2^38 reduce(lambda x,y:x ...

  7. permission is only granted to system apps--Android manifest权限问题

    在android的manifest.xml下编辑如下代码:<uses-permission android:name="android.permission.INTERNET" ...

  8. java基础day7

    1/匿名类对象:创建类的对象是匿名的. 比如说new Circle():就是一个匿名类对象. 匿名类对象只能使用一次. 2/形参:声明方法时,方法小括号内的参数 实参:调用方法是,实际传入的参数的值 ...

  9. 导入表数据 txt

    导入表数据 txt mysql> load data infile "D:/import.txt" into table shop;输出: Query OK, rows af ...

  10. Java开发环境安装

    一.安装JDK(java development kit) 下载地址:www.oracle.com/technetwork/java/javase/downloads 二.配置Java环境变量 1.J ...