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. JS简单实现图片切换

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  2. (转)SQL Server 2008将数据导出为脚本 [SQL Server]

    之前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然后运行就可以导出数据脚本了.现在在SQL Server 2008的Management Studio中增加了一个新特 ...

  3. 反射那些事儿——Java动态装载和反射技术

    一直以来反射都是只闻其声,却无法将之使用,近日尽心下来学习下,发现了很多精妙之处. Java动态装载和反射技术 一.类的动态装载 1.Java代码编译和执行的整个过程包含了以下三个重要的机制: ● J ...

  4. .net ADF 中 Ajax 的调用过程.

    图示是 .net ADF Ajax调用过程的简略过程: 1,2)当页面初始化之后, 浏览器一旦触发回调事件, 脚本函数负责处理回调信息, 并调用 ASP.NET 2.0/3.5 中的 WebForm_ ...

  5. XCode请求数据中接收类型的后台与前台处理(本机模拟)

      Xcode报错问题如下:   解决办法如下: 0x1 ->请求数据时加上缺少的类型 AFHTTPSessionManager *manager = [selfAFHTTPSessionMan ...

  6. 动态代理写connection连接池Demo

    public class JdbcUtil2 { //声明连接池<放到LinkedList中,操作其中对象的速度快 只需要改变连接> private static LinkedList&l ...

  7. HibernateTemplate常用方法总结

    HibernateTemplate常用方法 (本文章内容相当于转载自:http://www.tuicool.com/articles/fU7FV3,只是整理了一下内容结构和修改了部分内容,方便阅读) ...

  8. 出现并解决Navicat 报错:1130-host ... is not allowed to connect to this MySql server,MySQL

    例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话. GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY ...

  9. Python新手学习基础之运算符——成员运算与身份运算

    成员运算符 Python里有成员运算符,可以判断一个元素是否在某一个序列中.比如可以判断一个字符是否属于这个字符串,可以判断某个对象是否在这个列表中等等. Python中的成员操作符的使用语法是: o ...

  10. Sql数据保存到Excel文件中

    public string ExportExcel( DataSet ds,string saveFileName) { try { if (ds == null) return "数据库为 ...