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. hdu5067Harry And Dig Machine(TSP旅行商问题)

    题目链接: huangjing 题意:给出一幅图.图中有一些点,然后从第1个点出发,然后途径全部有石头的点.最后回到原点,然后求最小距离.当初作比赛的时候不知道这就是旅行商经典问题.回来学了一下. 思 ...

  2. ActiveReport 9手把手搭建环境及实战

    本文借鉴葡萄城空间产品,纯属分享,无任何营利目的 下载ActiveReports 9 专业版 打开下面的网站,点击立即下载,请您填写真实电子邮件地址,以获取ActiveReports产品试用版---系 ...

  3. VMware 虚拟机(linux)增加根目录磁盘空间 转自

    转自 http://wenku.baidu.com/link?url=WZDgESO0oXqYfhPYOWFalZsMglS0HKtLw7t6ICRs_sJ_sfPc85RpxsqKMwqSniis0 ...

  4. map的使用方法

    package cn.stat.p8.map.demo; import java.util.Collection; import java.util.HashMap; import java.util ...

  5. div+css不间断滚动字幕

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. canvas ---1

    Canvas1 (关键词:canvas)   canvas :就是html5中提供的一个标签,只是用来展示绘图的内容 canvas  标签的默认宽高:300*150 如果给canvas来设置高度和宽度 ...

  7. android图形基础知识

    Android核心分析(23)-----Andoird GDI之基本原理及其总体框架 2010-06-13 22:49 18223人阅读 评论(18) 收藏 举报 AndroidGDI基本框架 在An ...

  8. c# 与 c++ 编译

    C#的所有方法封装在类中,类的方法没有先后之分,无需声明.//而C++必须在函数调用前,由编译器检查参数类型是否合法,所以必须知道函数的原形(protype),所以必须提前声明函数的签名(signat ...

  9. 为什么针对XML的支持不够好?如何改进?

    为什么针对XML的支持不够好?如何改进? 物理文件是我们最常用到的原始配置的载体,最佳的配置文件格式主要由三种,它们分别是JSON.XML和INI,对应的配置源类型分别是JsonConfigurati ...

  10. Swift—计算属性-备

    计算属性本身不存储数据,而是从其他存储属性中计算得到数据. 计算属性概念: 计算属性提供了一个getter(取值访问器)来获取值,以及一个可选的setter(设置访问器)来间接设置其他属性或变量的值. ...