[rxjs] Shares a single subscription -- publish()
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()的更多相关文章
- [RxJS] Filtering operator: single, race
Single, race both get only one emit value from the stream. Single(fn): const source = Rx.Observable. ...
- RxJS库
介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...
- [译]Rxjs&Angular-退订可观察对象的n中方式
原文/出处: RxJS & Angular - Unsubscribe Like a Pro 在angular项目中我们不可避免的要使用RxJS可观察对象(Observables)来进行订阅( ...
- Thinking Clearly about Performance
http://queue.acm.org/detail.cfm?id=1854041 The July/August issue of acmqueue is out now acmqueue is ...
- paho-mqtt 学习笔记
Installation The latest stable version is available in the Python Package Index (PyPi) and can be in ...
- 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 ...
- Report launcher to run SSRS report subscriptions on demand
http://www.mssqltips.com/sqlservertip/3078/report-launcher-to-run-ssrs-report-subscriptions-on-deman ...
- hbot固件配置
又入了一台打印机,171到手,本来之前有更好的,无奈别人下手太快,只剩这台了. 175x135x180的样子. 创客的板,还带16g的闪迪内存卡,看到那会儿感觉赚大了! 拿到的时候不少螺丝松的,有的打 ...
- PostgreSQL源码安装文档
This document describes the installation of PostgreSQL using the source code distribution. (If yo ...
随机推荐
- Uva 1354 Mobile Computing
题目链接 题意: 在一个宽为r 的房间里, 有s个砝码, 每个天平的一端要么挂砝码, 要么挂另一个天平, 并且每个天平要保持平衡. 求使得所有砝码都放在天平上, 且总宽度不超过房间宽度的最大值. 思路 ...
- ipad在非viewport 1:1下缩放问题
1.最小会有980宽度,小于980应设置viewport 2.fix元素使用100%指定宽度时,默认会以min-width或980作为尺寸,可以选择给定与页面缩放时触发定宽来设置宽度,或设置设置bod ...
- bzoj 4031: [HEOI2015]小Z的房间 轮廓线dp
4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 98 Solved: 29[Submit][Status] ...
- Word添加新编号
要用到(1)(2),不想手写,但是word只有1.2.3和1)2)3),我就自定义哦.
- Z-stack之OSAL初始化流程
转自点击打开链接 我使用的协议栈版本及例子信息: ZigBee2006\Texas Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Samples\Sam ...
- 加JENKINS的SLAVE节点(LINUX)要注意的事项
从昨天下午到现在,终于解决了所有的问题.作如下记录要点: 1,从节点只要建好目录,JENKINS的包,MASTER会推送过来. 2,ANT,MAVEN之类的,要配置好环境变量,PROFILE.D .B ...
- java WebService简单使用案例
首先,建立一个WebService: package garfield; import javax.jws.WebService; import javax.xml.ws.Endpoint; @Web ...
- 17.2.2.1 The Slave Relay Log Slave中继日志
17.2.2.1 The Slave Relay Log Slave中继日志 中继日志, 像binary log,有一组文件组成包含events 描述数据库的修改,和一个index文件包含所有使用过的 ...
- WordPress RokIntroScroller插件‘thumb.php’多个安全漏洞
漏洞名称: WordPress RokIntroScroller插件‘thumb.php’多个安全漏洞 CNNVD编号: CNNVD-201309-383 发布时间: 2013-09-24 更新时间: ...
- HDU- 2265 Encoding The Diary
Encoding The Diary Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...