If have an observable and you subscribe it twice, those tow subscritions have no connection.

console.clear();
var Observable = Rx.Observable;
var _id = 1; var source = Observable.create(function(Observe){
var myId = _id++;
Observe.onNext('Observable ' + myId);
setTimeout(function(){
Observe.onNext('Observable... ' + myId);
Observe.onCompleted();
}, 1000);
}); var subscrition1 = source.subscribe(function onNext(x){
console.log('Observable 1: ' + x);
}); var subscrition2 = source.subscribe(function onNext(x){
console.log('Observable 2: ' + x);
});

Result:

/*"Observable 1: Observable 1"
"Observable 2: Observable 2"
"Observable 1: Observable... 1"
"Observable 2: Observable... 2"*/

publish():

Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence.

console.clear();
var Observable = Rx.Observable;
var _id = 1; var source = Observable.create(function(Observe){
var myId = _id++;
Observe.onNext('Observable ' + myId);
setTimeout(function(){
Observe.onNext('Observable... ' + myId);
Observe.onCompleted();
}, 1000);
});
var published = source.publish(); var subscrition1 = published.subscribe(function onNext(x){
console.log('Observable 1: ' + x);
}); var subscrition2 = published.subscribe(function onNext(x){
console.log('Observable 2: ' + x);
}); var connection = published.connect();

Results:

/*
"Observable 1: Observable 1"
"Observable 2: Observable 1"
"Observable 1: Observable... 1"
"Observable 2: Observable... 1"
*/

You can see the result just have one single subscrition then.

You can dispose the connection:

connection.dispose();

Results:

/*"Observable 1: Observable 1"
"Observable 2: Observable 1"*/

There is a problem when you connect the published observables at different place.

var Observable = Rx.Observable;
var _id = 1; var source = Observable.create(function(Observe){
var myId = _id++;
Observe.onNext('Observable ' + myId);
setTimeout(function(){
Observe.onNext('Observable... ' + myId);
Observe.onCompleted();
}, 1000);
});
var published = source.publish();
var connection = published.connect(); var subscrition1 = published.subscribe(function onNext(x){
console.log('Observable 1: ' + x);
}); var subscrition2 = published.subscribe(function onNext(x){
console.log('Observable 2: ' + x);
}); //var connection = published.connect();

Results:

/*"Observable 1: Observable... 1"
"Observable 2: Observable... 1"*/

If we move the connect() funciton up before subscribe(). Then we missed the very first console.log(); It means connection is already start, but no one subscribe it yet.

Therefore, we don't use publish() funciton alone, more than often we use publish().refCount() function together.

[rxjs] Shares a single subscription -- publish()的更多相关文章

  1. [RxJS] Filtering operator: single, race

    Single, race both get only one emit value from the stream. Single(fn): const source = Rx.Observable. ...

  2. RxJS库

    介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...

  3. [译]Rxjs&Angular-退订可观察对象的n中方式

    原文/出处: RxJS & Angular - Unsubscribe Like a Pro 在angular项目中我们不可避免的要使用RxJS可观察对象(Observables)来进行订阅( ...

  4. Thinking Clearly about Performance

    http://queue.acm.org/detail.cfm?id=1854041 The July/August issue of acmqueue is out now acmqueue is ...

  5. paho-mqtt 学习笔记

    Installation The latest stable version is available in the Python Package Index (PyPi) and can be in ...

  6. Using dijit/Destroyable to build safe Components

    In today's long-lived JavaScript apps it is essential to not introduce memory leaks within your cust ...

  7. Report launcher to run SSRS report subscriptions on demand

    http://www.mssqltips.com/sqlservertip/3078/report-launcher-to-run-ssrs-report-subscriptions-on-deman ...

  8. hbot固件配置

    又入了一台打印机,171到手,本来之前有更好的,无奈别人下手太快,只剩这台了. 175x135x180的样子. 创客的板,还带16g的闪迪内存卡,看到那会儿感觉赚大了! 拿到的时候不少螺丝松的,有的打 ...

  9. PostgreSQL源码安装文档

    This document describes the installation of PostgreSQL using the source    code distribution. (If yo ...

随机推荐

  1. UIWebView1-b

    随着H5的强大,hybrid app已经成为当前互联网的大方向,单纯的native app和web app在某些方面显得就很劣势.关于H5的发展史,这里有一篇文章推荐给大家,今天我们来学习最基础的基于 ...

  2. Computer Vision Applied to Super Resolution

    Capel, David, and Andrew Zisserman. "Computer vision applied to super resolution." Signal ...

  3. Pentaho Data Integration (三) Pan

    官网连接: http://wiki.pentaho.com/display/EAI/Pan+User+Documentation Pan Pan 是一个可以执行使用Spoon编辑的transforma ...

  4. C连接MySQL数据库开发之Linux环境完整示例演示(增、删、改、查)

    一.开发环境 ReadHat6.3 32位.mysql5.6.15.gcc4.4.6 二.编译 gcc -I/usr/include/mysql -L/usr/lib -lmysqlclient ma ...

  5. [cocos2d demo]认字小游戏

    2013.9.5更新第二版 游戏分三个场景,分别为主场景,加载场景以及游戏场景,游戏场景分为背景层,逻辑层以及UI层 1.背景:旋转太阳,移动波浪,漂浮的云 2.UI层:随机生成字附带在帆船上移动,当 ...

  6. 【转】JAVA字符串格式化-String.format()的使用--不错

    原文网址:http://blog.csdn.net/lonely_fireworks/article/details/7962171 常规类型的格式化 String类的format()方法用于创建格式 ...

  7. wpa_supplicant对wep,wpa,wpa2的psk和隐藏ap的scan_ssid扫描配置

    # 请不要修改下面这一行内容,否则将不能正常工作ctrl_interface=/var/run/wpa_supplicant # 确保只有root用户能读取WPA的配置ctrl_interface_g ...

  8. CodeViz产生函数调用图

    一.安装过程   1.CodeViz的安装 0).确认你已经安装Graphviz[ http://www.graphviz.org/] 1).下载CodeViz.[ http://www.csn.ul ...

  9. (转载)php循环检测目录是否存在并创建(循环创建目录)

    (转载)http://www.jb51.net/article/25917.htm php循环检测目录是否存在并创建,需要的朋友可以参考下. 循环创建目录方法 这个会生成image.gif目录 代码如 ...

  10. java Enumeration用法

    Enumeration是java.util中的一个接口类,在Enumeration中封装了有关枚举数据集合的方法. 在Enumeration中提供了方法hawMoreElement()来判断集合中是束 ...