[RxJS] Handling Multiple Streams with Merge
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的更多相关文章
- [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 ...
- [RxJS] Refactoring Composable Streams in RxJS, switchMap()
Refactoring streams in RxJS is mostly moving pieces of smaller streams around. This lessons demonstr ...
- [RxJS] Handling a Complete Stream with Reduce
When a stream has completed, you often need to evaluate everything that has happened while the strea ...
- [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( ...
- [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 ...
- [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 ...
- Visualize real-time data streams with Gnuplot
源文地址 (September 2008) For the last couple of years, I've been working on European Space Agency (ESA) ...
- [转]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 ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
随机推荐
- jquery之checkbox
//checkbox 数据回显 var publishRange=rowData.publishRange.split(","); for(var i = 0;i < pub ...
- Calendar( 日历)
本节课重点了解 EasyUI 中 Canlendar(日历)组件的使用方法,这个组件不依赖于其他组件.一. 加载方式//class 加载方式<div id="box" cla ...
- Droppable(放置)组件
.加载方式 //class 加载方式 <div id="dd" class="easyui-droppable" data-options="a ...
- log4net根据日志类型写入到不同的文件中
<?xml version="1.0"?> <configuration> <configSections> <!--log4net配置安 ...
- JavaScript中函数参数的按值传递与按引用传递(即按地址传递)
首先声明一句:JavaScript中所有函数的参数都是按值传递的!不存在按引用传递! 在讲传递参数之前我们先来讲一下指针. 学过C指针的应该都知道,指针变量中保存的是一个地址,程序可以根据所保存的地址 ...
- JavaScript绑定事件的方法[3种]
在JavaScript中,有三种常用的绑定事件的方法: 在DOM元素中直接绑定: 在JavaScript代码中绑定: 绑定事件监听函数. 一. 在DOM元素中直接绑定 这里的DOM元素,可以理解为HT ...
- MVC文件上传 - 使用Request.Files上传多个文件
控制器 1: using System; 2: using System.Collections.Generic; 3: using System.IO; 4: using System.Linq; ...
- 重新认识Intent
相信android开发工程师,对Intent一定不陌生,在整个开发中随时都用到了,今天我们总结一下Intent. 1. 为什么需要Intent? 在android Intent机制是协助应用间的交互与 ...
- n阶行列式计算----c语言实现(完结)
花了半天时间,写了这个n阶行列式计算的程序,应该算是比较优美吧,有很多地方多次做了优化,程序占用内存不是很大,要是说小吧,也不合适,因为里边有一个递归,而且递归的深度还比较深.时间复杂度具体没有细看, ...
- iOS学习之页面之间传值的方式总结
传值三种场景: 1.属性传值(从前往后传) 需求:第二个界面标签显示第一个界面输入框文字. 第一步, 在前一个界面定义属性. (语义属性声明为copy); 第二步, 在进入下一个界面之前,给属性传入数 ...