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. iOS - Responder Chain

     在iOS中,当发生事件响应时,必须知道由谁来响应事件.这就是由响应者链来对事件进行响应,所有事件响应的类都是UIResponder的子类,响应者链是一个由不同对象组成的层次结构,其中的每个对象将依次 ...

  2. iOS项目架构文档

    设计的项目架构主要引用MVVM+MVC架构,并以功能模块分级.以下为目录结构. 初级目录: 我们只需要关注SGZH文件夹下的目录,其他为Xcode管理的目录.可以看到此目录为项目初级目录,我们开发过程 ...

  3. MyEclipse过期激活方法

    如果已经过期会提示,进行购买.重新激活和退出,我们选择重新激活. 打开浏览器,地址栏输入key.858game.com,然后输入名称,在线获得MyEclipse的激活码. 输入Sumscripter: ...

  4. Ubuntu/Linux下7款轻量级编辑器 (转)

    From http://www.feiyan.info/39.html 在Windows卧铺使用Zend Studio或者EditPlus写PHP,Zend Studio适合大项目,EditPlus配 ...

  5. Python脚本调用C#代码数据交互示例(hello world)

    原地址: http://www.djangochina.cn/forum.php?mod=viewthread&tid=247 随着项目的逐渐收尾, 对IronPython脚本也越来越熟悉,这 ...

  6. mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用

    添加的作用是:指定字符的编码.解码格式.            例如:mysql数据库用的是gbk编码,而项目数据库用的是utf-8编码.这时候如果添加了useUnicode=true&cha ...

  7. git tag的使用

    查看所有的标签git tag 删除某一个标签git tag -d tagName 创建带注释的标签 git tag -a tagName -m "annotate" 轻量级标签 g ...

  8. 我泡在GitHub上的177天 by Ryan Seys

    我泡在GitHub上的177天   这是一个关于我如何连续177天(将近半年)泡在GitHub上不间断地贡献代码的故事.我会谈到我为什么要这么做,以及为什么你也应该效仿,或者至少做点类似的事情.这是一 ...

  9. Android——service重启

    一.在application中注册消息监听 public class BackgroundServiceApplication extends Application { @Override publ ...

  10. 遍历Dataset并输出数据实例

    转自:http://www.cnblogs.com/csdm/archive/2010/02/02/1661808.html <%@ Page Language="C#" A ...