[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. In its place, we will learn how to use the combineLatest operator.
const length$ = Rx.Observable.of(, );
const width$ = Rx.Observable.of(,);
const height$ = Rx.Observable.of(2.8, 2.5); const volume$ = Rx.Observable
.zip(length$, width$, height$,
(length, width, height) => length * width * height
); volume$.subscribe(function (volume) {
console.log(volume);
});
zip requiers each observable has synchronized emissions. It means:
const length$ = Rx.Observable.of();
const width$ = Rx.Observable.of();
const height$ = Rx.Observable.of(2.8, 2.5);
2.5 won't be calculated only when lenth$ and width$ provide second value.
In this case we can use combineLatest instead of zip:
const length$ = Rx.Observable.of();
const width$ = Rx.Observable.of();
const height$ = Rx.Observable.of(2.8, 2.5); const volume$ = Rx.Observable
.combineLatest(length$, width$, height$,
(length, width, height) => length * width * height
); volume$.subscribe(function (volume) {
console.log(volume);
});
One useful tip for using zip:
zip can spread sync value over time, when combine with interval
const source$ = of('hello')
const interval$ = interval().take()
source$.zip(interval$, (s, n) => s)
.subscribe()
/*
source$: (hello)|
interval$ ----0----1----2----3----4
zip
----h----e----l----l----o
*/
The same effect can also be done with concatMap + delay
const source$ = of('hello')
source$.concatMap(x => of(x).delay())
[RxJS] Replace zip with combineLatest when combining sources of data的更多相关文章
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- Angular快速学习笔记(4) -- Observable与RxJS
介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...
- python3 解决zip解压中文乱码问题,亲测可用, ZipFile
中文乱码是个很头疼的问题,找了好久都没用找到解决办法 后来也忘了在哪儿找到的解决办法, 很久以前了,但不可行, 解决了思路 在源码里面想要修改内容 if flags & 0x800: # UT ...
- rxjs入门6之合并数据流
一 concat,merge,zip,combineLatest等合并类操作符 以上操作符在版本6中已经只存在静态方法,不能在pipe中使用. import {concat,merge,zip,com ...
- 【转】Rxjs知识整理
原文:https://www.jianshu.com/p/16be96d69143 ---------------------------------------------------------- ...
- RxJS 6有哪些新变化?
我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6. rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的 ...
- java zip 工具类
原文:http://www.open-open.com/code/view/1430906539866 package com.topsoft.websites.utils; import java. ...
- Java 解压 zip 文件
代码如下 package test_java; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcep ...
- Like ruby of SBM Crusher zip to dict
how to use the zip to bulid a dict in python? data = """A dynamic, interpreted, open ...
随机推荐
- mysql允许外部连接设置
错误信息: SQL Error (1130): Host ‘192.168.1.88’ is not allowed to connect to this MySQL server 说明所连接的用户帐 ...
- JQuery DataTables 列自己定义数据类型排序
使用JQ DataTables 的时候.希望某列数据能够进行自己定义排序.操作例如以下:(以中文排序和百分比排序为例) 1:定义排序类型: //百分率排序 jQuery.fn.dataTableExt ...
- PHP 实现断点续传的原理和方法
PHP 实现断点续传的原理和方法 0. http协议从1.1开始支持静态获取文件的部分内容,为多线程下载和断点续传提供了技术支持.它通过在Header里两个参数实现的,客户端发请求时对应的是Accep ...
- Flume的可靠性
Flume的可靠性 当节点出现故障时,日志能够被传送到其他节点上而不会丢失. Flume提供了三种级别的可靠性保障,从强到弱依次分别为:end-to- end(收到数据agent首先将event写到磁 ...
- BZOJ3998: [TJOI2015]弦论(后缀自动机,Parent树)
Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个. ...
- 【习题 7-9 UVA-1604】Cubic Eight-Puzzle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] IDA* 保证这次移动的方格不和前一次重复. 然后加一个8数码的剪枝就行了. ->看看当前状态和目标状态有多少个地方是不一样的 ...
- Scala入门到精通——第二十九节 Scala数据库编程
本节主要内容 Scala Mavenproject的创建 Scala JDBC方式訪问MySQL Slick简单介绍 Slick数据库编程实战 SQL与Slick相互转换 本课程在多数内容是在官方教程 ...
- Google Web Toolkit(GWT) 在windows下环境搭建
1.什么是GWT? Google Web Toolkit(简称GWT,读作/ˈɡwɪt/),是一个前端使用JavaScript,后端使用Java的AJAX framework,以Apache许可证2. ...
- app 自动化测试 Appium+Java可以运行的代码
地址:http://www.cnblogs.com/sunny-sl/p/6520465.html
- 【习题 6-9 UVA - 127】"Accordian" Patience
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 链表模拟即可. 1pile不能加s... [代码] #include <bits/stdc++.h> using nam ...