[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 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
随机推荐
- Duanxx的技术问题:word界面显示模糊
今天打开word时,出现了word打开失败的现象,并且word的界面显示特别的模糊,找了好半天,才解决,问题见下图: 解决方式: 在word的文件->选项,这里面找到显示,然后勾选:禁用硬件图形 ...
- Python 日期和时间(转)
Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...
- WEB 开发所用的网站
常用前端手册: http://caniuse.com/ http://www.w3school.com.cn/ http://www.runoob.com/ http://www.css88.com/ ...
- uva 498 - Polly the Polynomial
UVa 498: Polly the Polynomial | MathBlog #include <cstdio> #include <cstdlib> using name ...
- JS中setAttribute的兼容性问题(摘自leejersey)
class和className兼容方法: object.setAttribute("class","content") 在IE8.Chrome.火狐.Opera ...
- 菜鸟做HTML5小游戏 - 刮刮乐
继上篇翻翻乐之后,又来刮刮乐.还是先上效果图: 开始demo的世界: 1.css去绘制界面效果.(源码提供) 2.原理:要实现刮刮卡内容的出现,我用div做了背景层去显示刮出的内容结果[重点].中间C ...
- 让PHP跑在Mac OS X中
MacBook入手了,配置工作环境,首先得让Mac OS支持PHP.不管你是采用集成的开发环境,比如XAMPP for Mac OS X,还是采用Mac OS中自带的Apache和PHP,甚至自己重新 ...
- php随机抽奖
貌似有些不合理,麻烦大家帮忙指正指正!谢谢~ <?php header("content-type:text/html;charset=utf-8"); function g ...
- PHP面向对象的构造方法与析构方法
构造方法与析构方法是对象中的两个特殊方法,它们都与对象的生命周期有关.构造方法时对象创建完成后第一个被对象自动调用的方法,这是我们在对象中使用构造方法的原因.而析构方法时对象在销毁之前最后一个被对象自 ...
- html meta标签属性用法集合
1.<meta name="generator" contect="">用以说明生成工具(如Microsoft FrontPage 4.0)等: 2 ...