[RxJS] Combining Streams with CombineLatest
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的更多相关文章
- [RxJS] Combining streams in RxJS
Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...
- [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. ...
- [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 ...
- [RxJS] Sharing Streams with Share
A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...
- 【转】Rxjs知识整理
原文:https://www.jianshu.com/p/16be96d69143 ---------------------------------------------------------- ...
- RxJS 6有哪些新变化?
我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6. rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的 ...
- RAC学习笔记
RAC学习笔记 ReactiveCocoa(简称为RAC),是由Github开源的一个应用于iOS和OS开发的新框架,Cocoa是苹果整套框架的简称,因此很多苹果框架喜欢以Cocoa结尾. 在学习Re ...
- ReactiveCocoa入门教程:第一部分
http://www.cocoachina.com/ios/20150123/10994.html 本文翻译自RayWenderlich,原文:ReactiveCocoa Tutorial--The ...
- 使用ReactiveCocoa实现iOS平台响应式编程
使用ReactiveCocoa实现iOS平台响应式编程 ReactiveCocoa和响应式编程 在说ReactiveCocoa之前,先要介绍一下FRP(Functional Reactive Prog ...
随机推荐
- WSAEventSelect
WSAEventSelect 是 WinSock 提供的一种异步事件通知I/O模型,与 WSAAsyncSelect模型有些类似. 该模型同样是接收 FD_XXX 之类的网络事件,但是是通 ...
- 数据持久化之CoreData
再次回归博客园, 已经实属不易了, 面临这近期忙忙碌碌的项目开发, 虽然并不是完全的没有闲暇时间, 但是怎么说呢, 也有着各种的无奈与曲折, 面临这产品需求的不断变化和页面的不断更新, 对于一个程序员 ...
- 大到可以小说的Y组合子(一)
问:上回乱扯淡了一通,这回该讲正题了吧. 答:OK. 先来列举一些我参考过,并从中受到启发的文章. (1.)老赵的一篇文章:使用Lambda表达式编写递归函数 (2.)装配脑袋的两篇文章:VS2008 ...
- Panel( 面板) 组件 上
一. 加载方式//class 加载方式<div class="easyui-panel" data-options="closable:true"titl ...
- jqueryMobile中select样式自定义
要去掉引入的jqueryMobile给下拉框组件的样式,有两种办法. 第一种:全局的去掉所有的下拉框样式: <link rel="stylesheet" href=" ...
- 获取GET/POST提交的数据,并处理中文问题
1.获取input标签中的值,用request.getParameter("User")(User为input的name值) 2. 获取checkbox的值,由于是多选的,所以不能 ...
- typedef std::string AddressLines[4]定义了一个string数组,大小为4
int main() { typedef std::]; std::]; std::string *pal1 = new AddressLines; delete [] pal; delete [] ...
- [目录][总结] C++和Java 中的主要操作对比
总结一些,C++ 和Java 中的一些常用操作对比,就当是自己的查询工具书啦.(暂时按随笔的更新时间排序) [Stack] c++ V.S. Java (2015.04.27) [Map] c++ ...
- 17--Box2D使用(三、触摸交互)
Box2D引擎与触摸的交互通过创建鼠标关节以及碰撞检测来得到触摸点下面的刚体,在根据触摸操作完成相应的功能.首先添加触摸响应函数声明 virtual void ccTouchesBegan(cocos ...
- .NET MVC权限控制
一.模块 权限控制的主体单位,与MVC里的Cotroller相对应. 二.模块权限 权限控制的最小单位,通过给每一个模块设置权限,可以控制到非常细小的功能. 三.角色 权限拥有者,通过给不同的角色分配 ...