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的更多相关文章

  1. [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 ...

  2. [RxJS] Introduction to RxJS Marble Testing

    Marble testing is an expressive way to test observables by utilizing marble diagrams. This lesson wi ...

  3. angular2 学习笔记 ( rxjs 流 )

    RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅,  ...

  4. rxjs的世界

    rxjs学习了几个月了,看了大量的东西,在理解Observable的本文借鉴的是渔夫的故事,原文,知识的主线以<深入浅出rxjs>为主,动图借鉴了rxjs中文社区翻译的文章和国外的一个动图 ...

  5. [RxJS] Marbles Testings

    Install: npm install — save-dev jasmine-marbles Basic example: import {cold, getTestScheduler} from ...

  6. [RxJS] Error Handling in RxJS

    Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thro ...

  7. Angular基础(八) Observable & RxJS

    对于一个应用来说,获取数据的方法可以有很多,比如:Ajax, Websockets, LocalStorage, Indexdb, Service Workers,但是如何整合多种数据源.如何避免BU ...

  8. RxJS v6 学习指南

    为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...

  9. Angular快速学习笔记(4) -- Observable与RxJS

    介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...

随机推荐

  1. cmd下运行java文件时,找不到或无法加载主类的解决方法

    最近再看java,却被一个看似很基础的hellorworld头疼了十几分钟,百度了一下,若你在cmd下编辑及运行.java文件时报错,解决方案如下,如图所示:

  2. POJ2103 Jackpot

    Description The Great Dodgers company has recently developed a brand-new playing machine. You put a ...

  3. 解决iOS应用内购买报错:invalidProductIdentifiers

    当写完IAP业务过程后,点击测试却发现没有返回成功的商品Id,反而返回了无效的商品:response.invalidProductIdentifiers 这种情况下考虑以下因素: 创建的App ID是 ...

  4. POJ 2992 Divisors

    每个数都可以分解成素数的乘积: 写成指数形式:n=p1^e1*p2^e2*...*pn^en:(p都是素数) 那么n的因数的数量m=(e1+1)*(e2+1)*...*(en+1): 所以用筛选法筛出 ...

  5. SQL Server 2005 版本的操作系统兼容性详细列表

    操作系统要求(32 位) 此表显示对于每种 32 位版本的 SQL Server 2005,操作系统是否可以运行其服务器软件. 有关如何在 Windows Server 2008 上安装 SQL Se ...

  6. kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.

    今天在写kafka生产者生成数据的程序并运行时,报如下错误: log4j:WARN No appenders could be found for logger (kafka.utils.Verifi ...

  7. c语言命名规则 [转载]

    C语言变量名命名规则 一.程序风格:         1.严格采用阶梯层次组织程序代码:         各层次缩进的分格采用VC的缺省风格,即每层次缩进为4格,括号位于下一行.     要求相匹配的 ...

  8. 【简译】Windows 线程基础

    翻译一篇关于windows线程的文章,原文在此.第一次翻译,如有错误请多指教 =========================================华丽的分割线============== ...

  9. Learning WCF 书中的代码示例下载地址

    Learning WCF Download Example Code 第一个压缩文件LearningWCF.zip是VS2005创建的项目,不要下载这个. 建议下载VS2008版的,以及Media

  10. WordPress 'is_serialized()'远程任意代码执行漏洞(CVE-2013-4338)

    漏洞版本: WordPress 3.6 漏洞描述: Bugtraq ID:62345 CVE ID:CVE-2013-4338 WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PH ...