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. shell 基础 $(cd `dirname $0`;pwd)

    $ cd `dirname $0` 和PWD%/* shell变量的一些特殊用法 在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的"." ...

  2. Android Studio的一些技巧和使用注意事项(持续更新)

    1.创建一个项目之后默认是没有assets目录的,可以手动在main目录下创建一个assets目录. 2.

  3. WEB 开发异常:java.lang.ClassNotFoundException

    某个类明明是有的,可是eclipse 启动tomcat服务器运行web项目,出现如题异常. java.lang.ClassNotFoundException 信息: Set web app root ...

  4. Handler sendMessage 与 obtainMessage (sendToTarget)

    这篇文章讲的很好: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 两种用法: 1. private void se ...

  5. 【HDOJ】1512 Monkey King

    左偏树+并查集.左偏树就是可合并二叉堆. /* 1512 */ #include <iostream> #include <string> #include <map&g ...

  6. JBPM4.4部署到tomcat6异常解决办法

    java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.ser ...

  7. 【转】win7 虚拟机virtualbox中ubuntu12.04安装samba实现文件共享

    原文网址:http://blog.csdn.net/watkinsong/article/details/8878786 昨天心血来潮,又装了个虚拟机,然后安装了ubuntu12.04,为了实现在虚拟 ...

  8. HDU 1280 前m大的数

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory L ...

  9. HDU-3719 二叉搜索树

    http://acm.hdu.edu.cn/showproblem.php?pid=3791 用数组建立二叉树: 二叉搜索树 Time Limit: 2000/1000 MS (Java/Others ...

  10. EF多表查询方式

    5, 连接 可以的连接有Join 和 GroupJoin 方法.GroupJoin组联接等效于左外部联接,它返回第一个(左侧)数据源的每个元素(即使其他数据源中没有关联元素). using (var ...