[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 ... 
随机推荐
- TypedArray和obtainStyledAttributes使用
			在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用TextView的配置属性: <com.easymorse.textbutto ... 
- NSRunLoop个人理解
			作者: xwang 出处: http://www.cnblogs.com/xwang/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保 ... 
- C#委托的回调机制
			代码如下: public partial class FrmMain : Form { // 定义回调使用关键字 delegate(回调是委托的一种应用,其本质就是委托) private delega ... 
- 使用VS Code开发AngularJS 2 第一个应用程序
			使用VS Code开发AngularJS 2 第一个应用程序 目录 运行环境 创建项目 安装依赖包 创建TypeScript应用程序 配置应用程序 运行应用程序 运行环境 运行环境: Windows ... 
- DG下手工处理v$archive_gap方法
			从9i以后,oracle dataguard 备库一般都不需要手工处理丢失的日志,FAL自动会帮我们处理,下面通过个案例来讲下手工处理丢失的日志的方法: 1.在备库查询有哪些日志丢失,没应用到备库 S ... 
- ORACLE中使用SQL的正则表达式判断邮箱格式
			在数据库中,有时需要判断字符串是否是一个或者多个邮箱格式,可以使用如下语句判断: ) FROM dual WHERE regexp_like(v_mail,'^\w+((-\w+)|(\.\w+))* ... 
- UIViewController的生命周期(图解)
			当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序1. alloc 创建对象,分配空间2.init (initWithNibName ... 
- ie6下:png图片不透明 和 背景图片为png的节点的内部标签单击事件不响应
			1.png图片不透明 少量图片时:使用滤镜: _background:none; _filter:prodig:DXImageTransform.Microsoft.AlphaImageLoader( ... 
- Backbone一些参考资源
			最近想找一个single-page JavaScript application Framework ,而不是单纯的Toolkit+Widget.来看YUI3的一段介绍: 引用 The YUI App ... 
- Android再学习-20141111-Android应用的七大件
			Android应用的七大件 应用程序的四大组件: Android的四大组件,使用时需要在程序中注册. Activity: Activity是应用程序的一个界面,可以通过这个界面查看联系人.打电话或者玩 ... 
