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. XenServer 使用笔记

    XenServer 模拟千兆网卡 这两天用 XenServer 安装 VM,其中一台 VM 是用作无盘测试的 Linux Server,不在主流发行版之列,无奈 XenServer 日前对非主流的 L ...

  2. 纯css实现幻灯片效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  3. c标签的使用方法

    1. c:forEach <c:forEach items="> 注意varStatus相当于for循环计数器,从1开始,用${varStatus.count}获得计数器的值.而 ...

  4. C#_Stopwatch 类

    命名空间:System.Diagnostics Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 Stopwatch 方案中,先调用 Start 方 ...

  5. poj3685 二分套二分

    F - 二分二分 Crawling in process... Crawling failed Time Limit:6000MS     Memory Limit:65536KB     64bit ...

  6. uva 105 - The Skyline Problem

    一.用数组储存该位置的最高点即可(图形的连续点离散化),注意左边界及右边界的情况: 注意:无论建筑物最左边是盖到哪里,你都得从1开始输出(输入输出都是integer,所以才能离散化): #includ ...

  7. 三星手机照相机出现了故障,htc无法连接无线网

    1 重启 2 若还不行,把内存卡,电池拿掉, 也可下载一个相机应用

  8. 第一个微信小程序(实现点击一个按钮弹出toast)

    今天根据网上的教程搭建了微信小程序的环境,然后看文档做了一个简单的小应用. 项目的目录是这个样子的: app.js.app.json.app.wxss是全局文件,必不可少的文件.定义在app.wxss ...

  9. 用js 做大图轮播方法(一)

    //html部分 <div id="wrap"> <div id="slider"> <a target="_blank ...

  10. css 设置字体

    CSS,font-family,好看常用的中文字体 例1(小米米官网):font-family: "Arial","Microsoft YaHei"," ...