[RxJS] Changing Behavior with MapTo
You often need streams to trigger different behaviors on the data based on which streams triggers. This lessons shows how to use mapTo to pass functions into the scan operator and have completed control over you data.
Current Code:
const Observable = Rx.Observable;
const startButton = document.querySelector('#start');
const stopButton = document.querySelector('#stop');
const start$ = Observable.fromEvent(startButton, 'click');
const interval$ = Observable.interval(1000);
const stop$ = Observable.fromEvent(stopButton, 'click');
const intervalThatStops$ = interval$
.takeUntil(stop$);
const inc = (acc) => ({count: acc.count + 1}); // one line arrow function only ruturn object need ()
const data = {count: 0};
start$
.switchMapTo(intervalThatStops$)
.startWith(data)
.scan( inc )
.subscribe((x)=> console.log(x));
Everytime the number 1,2,3... will be passed to the scan function.
If we want scan() method be fixable enought, we can use mapTo() method, which accecpts a function to increase the number. Then we need to modify the scan() function, now everytime it receive is the function return from mapTo, not a number anymore.
start$
.switchMapTo(intervalThatStops$)
.mapTo( inc )
.startWith(data)
.scan( (acc, curr) => {
console.log(curr); //(acc) => ({count: acc.count + 1})return curr(acc)
} )
.subscribe((x)=> console.log(x));
Now we get full control over the scan() method, we can let it reset after 10:
const Observable = Rx.Observable;
const startButton = document.querySelector('#start');
const stopButton = document.querySelector('#stop');
const start$ = Observable.fromEvent(startButton, 'click');
const interval$ = Observable.interval(200);
const stop$ = Observable.fromEvent(stopButton, 'click');
const intervalThatStops$ = interval$
.takeUntil(stop$);
const data = {count: 0};
const inc = (acc) => { return Object.assign({}, data, {count: count + 1})}; // avoid modifying data object
const resetAfterTen = (acc) => {
if(acc.count == 10){
return data;
}else{
return Object.assign({}, acc, {count: acc.count + 1})
}
}
start$
.switchMapTo(intervalThatStops$)
.mapTo( resetAfterTen )
.startWith(data)
.scan( (acc, curr) => {
return curr(acc)
} )
.subscribe((x)=> console.log(x));
[RxJS] Changing Behavior with MapTo的更多相关文章
- [Reactive Programming] RxJS dynamic behavior
This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...
- Behavior Trees for Path Planning (Autonomous Driving)
Behavior Trees for Path Planning (Autonomous Driving) 2019-11-13 08:16:52 Path planning in self-driv ...
- 构建自动化前端样式回归测试——BackstopJS篇
在使用scss和less开发的时候,遇到过一件很有趣的事,因为网站需要支持响应式,就开了一个响应式样式框架,简单的几百行scss代码,居然生成了近100KB的css代码,因此决定重构这个样式库.而重构 ...
- Devexpress Winform MVVM
归纳总结备忘 Devexpress Winform MVVM Practice 前言 MVVM Devexpress 正文 databindings及 UI Triggers Command 委托Co ...
- XCTest(二)
New tool sets are making it easier and easier to engage in genuine agile development on iOS. In part ...
- DevExpress MVVM<1>
DevExpress MVVM 概念 模型 -定义数据和您的业务逻辑. 视图 -指定UI,包括绑定到ViewModel中的属性和命令的所有可视元素(按钮,标签,编辑器等). ViewModel-连接模 ...
- 自家公司关于git commit 的规范
代码提交的commit info提个建议,fix的issue是哪个issue?都要有明确的链接.推荐方式:1.建立issue,说明问题的背景和原因.http://git.startdt.net/pay ...
- [RxJS] Transformation operator: map and mapTo
We made our first operator called multiplyBy, which looks a bit useful, but in practice we don't nee ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
随机推荐
- SVN服务器的本地搭建和使用
用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用. 详细步骤如下 http://www.2cto.com/os/201412/361931.html
- C#基础语法总结
C#笔记——基础篇 一.入门知识 VS中的常用快捷键 Ctrl+K+D:快速对齐代码 Ctrl+Z:撤销 Ctrl+S:保存(一定要经常保存!) Ctrl+J:快速弹出智能提示 Shift+End . ...
- 网页、JavaScript 数据类型
JavaScript 数据类型 一.基本数据类型: 字符串.数字.布尔.日期和时间 JavaScript 拥有动态类型 JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: 1 v ...
- JAVA 泛型练习
二分查找: public class Q212 { public static void main(String [] args) { Integer []arr = {1,2,3,4,5,6,7,8 ...
- Arcgis - Personal Geodatabase 和 File Geodatabase的区别.
一.平台支持: 1.Personal Geodatabase:仅可在Windows 上运行: 2.File Geodatabase:跨平台支持,可在Windows 及UNIX.linux上运行. 评 ...
- MySql事务及隔离级别
在数据库中,所谓事务是指作为单个逻辑工作单元执行的一系列操作. 事务的操作: 先定义开始一个事务,然后对数据作修改操作, 这时如果提交(COMMIT),这些修改就永久地保存下来 如果回退(ROLLBA ...
- web标准(复习)--6 html列表
今天我们开始学习html列表,包含以下内容和知识点: ul无序和ol有序列表 改变项目符号样式或用图片定义项目符号 横向图文列表 浮动后父容器高度自适应 IE6的双倍边距bug 一.ul无序和ol有序 ...
- 转载:spring ,struct2 在 web.xml中的配置
转载网址:http://blog.sina.com.cn/s/blog_4c6e822d0102dv63.html <!-- Struts2 need begin--> <filt ...
- POJ1850 组合数学
POJ1850 问题重述: 用26个小写字母进行编码,编码规则如下: 1)每个编码中前一个字母必须小于后一个字母 2)编码按照长度从小到大排列,相同长度按字典序进行排列 输入一个字母串,求解该编码对应 ...
- jQuery实现的全选、反选和不选功能
适用于网页多选后需要进行批量操作的场景(如批量删除等).如有问题希望大家可以指正.谢谢~~ HTML 我们的页面上有一个歌曲列表,列出多行歌曲名称,并匹配复选框供用户选择,并且在列表下方有一排操作按钮 ...