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. OC中格式化输出符号

    定义 说明 %@ Objective-C object, printed as the string returned by descriptionWithLocale: if available, ...

  2. Memcached(三)Memcached配置参数初解

    一.基本参数在我们第一次安装Memcached时,一般都是用过这个命令: memcached -m 512 -u root -d -l 127.0.0.1 -p 11211 我们先来解释这几个参数的含 ...

  3. 转:Redis Geo: Redis新增位置查询功能

    原文来自于:http://www.infoq.com/cn/news/2015/07/redis-geo 移动互联网增进了人与人之间的联系,其中基于位置信息的服务(Location Based Ser ...

  4. Steam游戏黑屏与游戏直接安装方法

    黑屏原因主要是360禁止了反作弊进程,解决方法: Steam游戏目录: Steam\steamapps\common\*** 例如黎明杀机,备份时候可直接备份该目录下 Dead by Daylight ...

  5. MySQL安装(以程序的方式启动)zip版

    电脑环境 win10 64位 企业版 1.解压zip文件到某路径下. 2.复制目录下的my-default.ini 改名字(my.ini) 3.打开my.ini [mysqld]下面加上charact ...

  6. 超级 Ping 监测工具——为您的网络状态保驾护航

    关于 Ping Ping 是一个网络命令,主要是用于确定本地主机是否能与另一台主机交换(发送与接收)数据.根据返回的信息,就可以推断 TCP/IP 参数是否设置得正确以及运行是否正常.正常情况下,Pi ...

  7. SessionId

    http://www.codeweblog.com/session-cookie-jsessionid-url-rewriting/

  8. 创建支持CRUD(增删改查)操作的Web API(二)

    一:准备工作 你可以直接下载源码查看 Download the completed project.     下载完整的项目 CRUD是指“创建(C).读取(R).更新(U)和删除(D)”,它们是四个 ...

  9. 利用spm提供的MoAEpilot听觉数据学习预处理以及单被试glm分析与统计推断

    1.数据介绍 下载:http://www.fil.ion.ucl.ac.uk/spm/data/auditory/ SUBJECT:1 VOLUME: 64*64*64 TR:7s total acq ...

  10. 【转】Valid signing identity not found解决办法(原有IDP私钥丢失)及Certificate、App ID、Devices、Provisioning Profiles之间区别--不错

    原文网址:http://blog.csdn.net/mad1989/article/details/8699147 前言: 刚刚把mini换成了macbookair,之前一直在mini上进行开发,到换 ...