[rxjs] Demystifying Cold and Hot Observables in RxJS
Cold:
console.clear();
var Observable = Rx.Observable;
var clock = Observable.interval(1000).take(10).map((i) => `${i}!`);
clock.subscribe((x) => {
console.log(` a ${x}`);
}); setTimeout(function(){
clock.subscribe((x) => {
console.log(` b ${x}`);
});
}, 3500);
Results:
/*
" a 0!"
" a 1!"
" a 2!"
" a 3!"
" b 0!"
" a 4!"
" b 1!"
" a 5!"
" b 2!"
" a 6!"
" b 3!"
" a 7!"
" b 4!"
" a 8!"
" b 5!"
" a 9!"
" b 6!"
" b 7!"
" b 8!"
" b 9!"
*/
As you can see, 'a' and 'b' all start from '0'. They are independent. As youtube vedio, you can open the same vedio in tow tabs. When you click play, those two vedio will play independently.
Hot: publish().refCount();
Hot Observables are like 'live' youtube video, everyone watch the same vedio at the same pace.
As I wrote in previous article about publish(); you can use this with connect() funciton, but there is problem, we will miss the very first event.
RefCount and a hot observable is analogous to a live video of a band playing at a concert, but the band doesn't start playing if there isn't anyone in the audience. That would be a waste, right? So, why play if there is no one watching?
RefCount tells the band to play when there is at least one person in the audience, in other words, when the number of observers goes from zero to one.
console.clear();
var Observable = Rx.Observable;
var clock = Observable.interval(1000).take(10).map((i) => `${i}!`).publish().refCount();
clock.subscribe((x) => {
console.log(` a ${x}`);
}); setTimeout(function(){
clock.subscribe((x) => {
console.log(` b ${x}`);
});
}, 3500);
Results:
/*" a 0!"
" a 1!"
" a 2!"
" a 3!"
" b 3!"
" a 4!"
" b 4!"
" a 5!"
" b 5!"
" a 6!"
" b 6!"
" a 7!"
" b 7!"
" a 8!"
" b 8!"
" a 9!"
" b 9!"
*/
[rxjs] Demystifying Cold and Hot Observables in RxJS的更多相关文章
- [Vue-rx] Switch to a Function which Creates Observables with Vue.js and Rxjs
Wrapping the creation of an Observable inside of a Function allows you delay the creation of the Obs ...
- [RxJS] Introduction to RxJS Marble Testing
Marble testing is an expressive way to test observables by utilizing marble diagrams. This lesson wi ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
- rxjs的世界
rxjs学习了几个月了,看了大量的东西,在理解Observable的本文借鉴的是渔夫的故事,原文,知识的主线以<深入浅出rxjs>为主,动图借鉴了rxjs中文社区翻译的文章和国外的一个动图 ...
- [RxJS] Marbles Testings
Install: npm install — save-dev jasmine-marbles Basic example: import {cold, getTestScheduler} from ...
- [RxJS] Error Handling in RxJS
Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thro ...
- Angular基础(八) Observable & RxJS
对于一个应用来说,获取数据的方法可以有很多,比如:Ajax, Websockets, LocalStorage, Indexdb, Service Workers,但是如何整合多种数据源.如何避免BU ...
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- Angular快速学习笔记(4) -- Observable与RxJS
介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...
随机推荐
- 学习TextKit框架(上)
TextKit简介 在iOS7之前我们要实现图文混排要使用CoreText,iOS6时有了Attribute string 可以解决一些简单的富文本需求.直到iOS7 苹果推出了TextKit,Tex ...
- php实例-正则获取网站音频地址的实例(Listen to this 1)
主要用到了:file_get_contents();preg_match_all(); 这2个函数 查看地址:http://git.oschina.net/xiaoz6/phpExample
- 引用Excel时 未在本地计算机上注册ace.oledb.12.0
可能由于未安装数据库补丁 下载地址http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/A ...
- APP二维码微信扫描后无法下载的问题
微信打开网址添加在浏览器中打开提示 2014-10-13 蔡宝坚 移动前端 使用微信打开网址时,无法在微信内打开常用下载软件,手机APP等.网上流传的各种微信打开下载链接,微信已更新基本失效,最 ...
- ubuntu 64bit “arm-linux-gcc: No such file or directory”问题的解决方法
安装lsb-core sudo apt-get install lsb-core
- 制作qtopia-2.2.0和qt4文件系统
转自 rootfs_qtopia_qt4.img 1. 解压rootfs_qtopia_qt4-20100816.tar.gz,得到目录rootfs_qtopia_qt4,里面内容比较大,超过了64M ...
- 好吧,如果一定要RESTFUL的DJANGO
看看人家写的VIEWSET, 然后用REQUESTS测试一下. from rest_framework import viewsets from rest_framework.decorators i ...
- WEB 开发异常:java.lang.ClassNotFoundException
某个类明明是有的,可是eclipse 启动tomcat服务器运行web项目,出现如题异常. java.lang.ClassNotFoundException 信息: Set web app root ...
- "NetworkError: 500 Internal Server Error - http://develop.console.aliyun.sinopec.com/ots/ots_queryOtsList.action?state=0"
项目明明开始好好的,报的这个错,错误提示也很少,啥信息都没有,只是明白是服务器报了500,知道是服务器内部错误,但是却没法找不到问题所在.后来突然想到把下面报错的action直接在浏览器运行: htt ...
- SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace
1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...