Two streams often need to work together to produce the values you’ll need. This lesson shows how to use an input stream and an interval stream together and push an object with both values through the stream.

const Observable = Rx.Observable;

const startButton = document.querySelector('#start');
const halfButton = document.querySelector('#half');
const quarterButton = document.querySelector('#quarter');
const input = document.querySelector("#input");
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 half$ = Observable.fromEvent(halfButton, 'click');
const quarter$ = Observable.fromEvent(quarterButton, 'click');
const stop$ = Observable.fromEvent(stopButton, 'click');
const reset$ = Observable.fromEvent(resetButton, 'click');
const starters$ = Observable.merge(
start$.mapTo(1000),
half$.mapTo(500),
quarter$.mapTo(250)
);
const intervalActions = (time) => {
return Observable.merge(
Observable.interval(time)
.takeUntil(stop$)
.mapTo(inc),
reset$.mapTo(reset)
)
};
const timer$ = starters$
.switchMap(intervalActions)
.startWith(data)
.scan( (acc, curr) => curr(acc));
const input$ = Observable.fromEvent(input, "input")
.map(ev=>ev.target.value); Observable.combineLatest(
timer$,
input$
)
// We will get an array combineLatest
.map((array)=>{
return {count: array[0].count, input: array[1]}
})
.subscribe(x => console.log(x))

They last param of combineLatest is a result function, which can parse the result in the fucntion:

Observable.combineLatest(
timer$,
input$,
(timer, input)=>{
return {count: timer.count, input}
}
)
.subscribe(x => console.log(x))

[RxJS] Combining Streams with CombineLatest的更多相关文章

  1. [RxJS] Combining streams in RxJS

    Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...

  2. [RxJS] Replace zip with combineLatest when combining sources of data

    This lesson will highlight the true purpose of the zip operator, and how uncommon its use cases are. ...

  3. [RxJS] Aggregating Streams With Reduce And Scan using RxJS

    What is the RxJS equivalent of Array reduce? What if I want to emit my reduced or aggregated value a ...

  4. [RxJS] Sharing Streams with Share

    A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...

  5. 【转】Rxjs知识整理

    原文:https://www.jianshu.com/p/16be96d69143 ---------------------------------------------------------- ...

  6. RxJS 6有哪些新变化?

    我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6.  rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的 ...

  7. RAC学习笔记

    RAC学习笔记 ReactiveCocoa(简称为RAC),是由Github开源的一个应用于iOS和OS开发的新框架,Cocoa是苹果整套框架的简称,因此很多苹果框架喜欢以Cocoa结尾. 在学习Re ...

  8. ReactiveCocoa入门教程:第一部分

    http://www.cocoachina.com/ios/20150123/10994.html 本文翻译自RayWenderlich,原文:ReactiveCocoa Tutorial--The ...

  9. 使用ReactiveCocoa实现iOS平台响应式编程

    使用ReactiveCocoa实现iOS平台响应式编程 ReactiveCocoa和响应式编程 在说ReactiveCocoa之前,先要介绍一下FRP(Functional Reactive Prog ...

随机推荐

  1. Intellj IDEA 启动参数调优

    (修改前记得备份) 修改IntellJ/bin/idea.exe.vmoptions修改成 -Xms512m -Xmx512m -Xmn164m -XX:MaxPermSize=250m -XX:Re ...

  2. 前端笔试题目总结——应用JavaScript函数递归打印数组到HTML页面上

    数组如下: var item=[{ name:'Tom', age:70, child:[{ name:'Jerry', age:50, child:[{ name:'William', age:20 ...

  3. Java NIO框架Netty教程(一) – Hello Netty

    先啰嗦两句,如果你还不知道Netty是做什么的能做什么.那可以先简单的搜索了解一下.我只能说Netty是一个NIO的框架,可以用于开发分布式的Java程序.具体能做什么,各位可以尽量发挥想象.技术,是 ...

  4. iPhone手机的屏幕尺寸、分辨率及适配

    1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...

  5. cas+tomcat+shiro实现单点登录-3-CAS服务器深入配置(连接MYSQL)

    目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ...

  6. poj2486

    题目大意:给出一个树,每个节点有一个值,问从1开始走走k步最多能遍历到最大的值的和:dp[i][j][k] 当i为零是表示如果从j点开始遍历以j为跟的子树,走k步后回到原点的最大值.dp[i][j][ ...

  7. Centos6.5使用yum安装MariaDB

    系统版本:Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x8 ...

  8. c# 基础复习1

    1. 类和对象 1.1 类和对象的概念 类:对象的类型,它不同于 int 等基本数据类型,因为类具有行为:也可以说是具有相 同特征和行为的一组对象的集合. 对象:对象是一个个你能看得见,摸得着的实体, ...

  9. php 之 文件操作(0524)

    php中文件包含两种:文件,文件夹.文件夹又称目录 新建一个文件aa.txt和一个文件夹text,text文件夹下又包含bb.txt 一.判断文件类型filetype("./aa.txt&q ...

  10. 《图解CSS3》——笔记(一)

    作者:大漠 勘误:http://www.w3cplus.com/book-comment.html 2014年7月14日14:46:35 第一章  揭开CSS3的面纱 1.1  什么是CSS3 CSS ...