[RxJS] Adding Conditional Logic with Filter
Often you only want values to proceed through your stream if they meet certain criteria, just as if you were using an if statement in plain JavaScript. This lesson shows you how to use filter on your stream to only push the values that you need through your stream.
const Observable = Rx.Observable;
const startButton = document.querySelector('#start');
const halfButton = document.querySelector('#half');
const quarterButton = document.querySelector('#quarter');
const stopButton = document.querySelector('#stop');
const resetButton = document.querySelector('#reset');
const input = document.querySelector('#input');
const start$ = Observable.fromEvent(startButton, 'click');
const half$ = Observable.fromEvent(halfButton, 'click');
const quarter$ = Observable.fromEvent(quarterButton, 'click');
const stop$ = Observable.fromEvent(stopButton, 'click');
const reset$ = Observable.fromEvent(resetButton, 'click');
const input$ = Observable.fromEvent(input, 'input')
.map(event => event.target.value);
const data = {count:};
const inc = (acc)=> ({count: acc.count + });
const reset = (acc)=> data;
const starters$ = Observable.merge(
start$.mapTo(),
half$.mapTo(),
quarter$.mapTo()
);
const intervalActions = (time)=> Observable.merge(
Observable.interval(time)
.takeUntil(stop$).mapTo(inc),
reset$.mapTo(reset)
);
const timer$ = starters$
.switchMap(intervalActions)
.startWith(data)
.scan((acc, curr)=> curr(acc))
Observable.combineLatest(
timer$,
input$,
(timer, input)=> ({count: timer.count, text: input})
)
.filter((data)=> data.count === parseInt(data.text))
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);
[RxJS] Adding Conditional Logic with Filter的更多相关文章
- [Ramda] Handle Branching Logic with Ramda's Conditional Functions
When you want to build your logic with small, composable functions you need a functional way to hand ...
- angular7 Rxjs 异步请求
Promise 和 RxJS 处理异步对比 Promise 处理异步: let promise = new Promise(resolve => { setTimeout(() => { ...
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- Adding Form Fields to a MS Word Document
Configuring a Word Merge in SmartSimple is a three-step process: Create the MS Word document that wi ...
- [RxJS] Learn How To Use RxJS 5.5 Beta 2
The main changes is about how you import rxjs opreators from now on. And introduce lettable opreator ...
- RxJS——Operators
RxJS 的操作符(operators)是最有用的,尽管 Observable 是最基本的.操作符最基本的部分(pieces)就是以申明的方式允许复杂的异步代码组合简化. 什么是操作符? 操作符是函数 ...
- Programming Entity Framework 翻译(1)-目录
1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- 译:Spring框架参考文档之IoC容器(未完成)
6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...
随机推荐
- VS2010中属性页中,C/C++ -->预处理器定义
如上图中,在这里,WIN32._DEBUGE._UNICODE等其实是一些宏定义,在这里写上这些,相当于在本工程所有的文件中都写上了: #define WIN32 #define _DEBUG#def ...
- android离线下载的相关知识
离线下载的功能点如下: 1.下载管理(开始.取消下载). 2.网络判断(Wi-Fi,3G). 3.独立进程. 4.定时和手机催醒. 5.自启动. 选择 ...
- XtraBackup做mysql主从同步
一.背景: 线上一个主库压力比较大,所以增加一个从库,但是不能重启或者停止主库的正常运行,不能锁库锁表影响业务的正常运行.所以这里想到了XtraBackup 二.XtraBackup介绍: Xtrab ...
- ComboGrid( 数据表格下拉框)
一. 加载方式//class 加载方式<select id="box" class="easyui-combogrid" name="dept& ...
- NumberBox( 数值输入框) 组件
本节课重点了解 EasyUI 中 NumberBox(数值输入框)组件的使用方法,这个组件依赖于 ValidateBox(验证框)组件.一. 加载方式//class 加载方式<input typ ...
- HTML与CSS入门——第六章 使用字体
知识点: 1.粗体.斜体和特殊文本格式的使用 2.字体的调整方法 3.特殊字符的使用方法 6.1 粗体.斜体和特殊文本格式: font-weight控制粗细 加粗<strong> font ...
- C#socket通讯两个最经典错误解决方案
1.经典错误之 无法访问已释放的对象. 对象名:“System.Net.Sockets.Socket” (1).问题现场 (2).问题叙述 程序中的某个地方调用到了socket.close ...
- ORA-00911: invalid character
出错原因:sql语句后面加了中文状态下的分号. 解决办法:改成英文状态下的分号即可. --本篇文章参考自:http://blog.sina.com.cn/s/blog_5b2a1aee0100n4oy ...
- IDEA 运行 控制台显示一堆乱码,一些问号
如下: 问题: 配置程序运行参数的时候,配置到了"VM options",应该为配置到"Program arguments",如图:
- uva12486 Space Elevator(数位dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题目链接:https://uva.onlinejudge.org/index.ph ...