[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) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...
随机推荐
- 【python之路7】python基本数据类型(一)
一.运算符 1.算数运算符 +.-.*./.%(求余数).//(取整数部分) python2.x中,如果计算浮点数其中一个数字必须是浮点数否则按整数计算: 如python2.7中:print 9/2 ...
- webservice 的权限验证
1. http://blog.csdn.net/jaune161/article/details/25602655 2. http://wcp88888888.iteye.com/blog/13993 ...
- SQL server 开启 cmdshell
GO RECONFIGURE GO GO RECONFIGURE GO EXEC master..xp_cmdshell 'net use Z: \\192.168.11.1\192.168.11.4 ...
- ANDROID_MARS学习笔记_S02_014_GSON解析JSON串为对象
package com.json2; import android.app.Activity; import android.os.Bundle; import android.view.View; ...
- Handler sendMessage 与 obtainMessage (sendToTarget)
这篇文章讲的很好: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 两种用法: 1. private void se ...
- zip压缩解压缩 项目icsharpcode-SharpZipLib-e012155
大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,支持Zip, GZip, BZip2 和Tar格式 ...
- Oracle正则表达式
Oracle正则表达式 正则表达式具有强大.便捷.高效的文本处理功能.能够添加.删除.分析.叠加.插入和修整各种类型的文本和数据.Oracle从10g开始支持正则表达式. 下面通过一些例子来说明 ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- ThreadPoolExecutor原理及使用
大家先从ThreadPoolExecutor的总体流程入手: 针对ThreadPoolExecutor代码,我们来看下execute方法: public void execute(Runnable c ...
- Muduo源码库研究(笔记汇总)
声明: 本人学习Muduo源码, 有些代码会对其进行精简, 加上本人的一些理解, 所以与作者的代码可能有些不同. 如有理解错误的地方欢指出. Muduo基础库-时间戳类 http://www.cnbl ...