[RxJS] Refactoring CombineLatest to WithLatestFrom
This lesson shows why it’s preferable to using withLatestFrom instead of combineLatest in certain scenarios.
Timer will continue until you enter the number in the input field:
timer$
.do((x)=> console.log(x))
.combineLatest(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.takeWhile((data)=> data.count <= )
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , )
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);
In this case, withLatestFrom() works the same way:
timer$
.do((x)=> console.log(x))
.withLatestFrom(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.takeWhile((data)=> data.count <= )
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , )
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);
But let's say we only want the timer log out 3 times then it should hit the complete block, logout "complete":
timer$
.do((x)=> console.log(x))
.takeWhile((data)=> data.count <= )
.withLatestFrom(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , )
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);
then it only works with withLatestFrom() NOT combimeLatest().
The reason for that is combimeLatest require both timer$ and input$. But withLatestFrom() only need $timer.
[RxJS] Refactoring CombineLatest to WithLatestFrom的更多相关文章
- [RxJS] Refactoring Composable Streams in RxJS, switchMap()
Refactoring streams in RxJS is mostly moving pieces of smaller streams around. This lessons demonstr ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
- [RxJS] Combining streams in RxJS
Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- rxjs的世界
rxjs学习了几个月了,看了大量的东西,在理解Observable的本文借鉴的是渔夫的故事,原文,知识的主线以<深入浅出rxjs>为主,动图借鉴了rxjs中文社区翻译的文章和国外的一个动图 ...
- 《深入浅出RxJS》读书笔记
rxjs的引入 // 如果以这种方式导入rxjs,那么整个库都会导入,我们一般不可能在项目中运用到rxjs的所有功能 const Rx = require('rxjs'); 解决这个问题,可以使用深链 ...
- [RxJS] Combination operator: zip
CombineLatest and withLatestFrom are both AND-style combination operators. In this lesson, we will l ...
- [RxJS] Combination operator: withLatestFrom
Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...
- RxSwift学习笔记10:startWith/merge/zip/combineLatest/withLatestFrom/switchLatest
//startWith //该方法会在 Observable 序列开始之前插入一些事件元素.即发出事件消息之前,会先发出这些预先插入的事件消息 Observable.of(1,2,3) .startW ...
随机推荐
- Android实用代码块
通过反射实现Menu显示图标 @Override public boolean onCreateOptionsMenu(Menu menu) { setIconEnable(menu, true); ...
- office2010删除多余空行
选择 ctrl+H,弹出 "查找和替换"对话框,在"查找内容"输入"^p^p",并在"替换为"输入"^p&qu ...
- #define 和 typedef场合
#define定义“可读”的常量以及一些宏语句的任务,而typedef则常用来定义关键字.冗长的类型的别名.
- wsdl和wadl区别
[转]http://blog.csdn.net/liuxiao723846/article/details/51611183 1.Java开发WebService最重要的两个规范: JSR-224 ( ...
- JavaScript--Json对象
JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任何 ...
- Mybatis学习总结(二)—使用接口实现数据的增删改查
在这一篇中,让我们使用接口来实现一个用户数据的增删改查. 完成后的项目结构如下图所示: 在这里,person代表了一个用户的实体类.在该类中,描述了相关的信息,包括id.name.age.id_num ...
- 快速入门cocos2d-x jsbinding
如果你是一个cocos2d-x的老手,那你可以忽略这篇博文,如果你是一个接触过javascript,想通过HTML5做游戏的,但是苦于不知道如何下手,那么这篇博文可能会帮到你. cocos2dx-js ...
- git clone 远程分支
先初始化一个git 仓库 命令:git init git clone 相应的地址 这样就会形成一个.git 隐藏文件夹 一定要注意的,要进入到子文件夹去git checkout feature/0. ...
- Java在ACM中的使用
1.基本框架 import java.oi.*; import java.util.* public class Main { public static void main(St ...
- Java内部类和外部类的通信探索
1.内部类访问外部类的成员和方法 在内部类中,可以无障碍地访问外部类的所有成员和方法. 在下面的实验代码中,可以看到,内部类sl可以访问外部类的私有成员:sz 和 cur. 同时可以访问私有方法:pr ...