[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 ...
随机推荐
- 一些嵌入式和FPGA相关模块的开源
工作一年,整理下手头做过的东西,分享出来,希望能帮到大家. 嵌入式方面,主要集中在Xilinx家的器件上,ZYNQ居多.Linux相关的就不贴了,网上的资料太多,xilinx-wiki上资料都是比较全 ...
- xmpp 配置数据库 服务器
一.了解XMPP 协议(标准) XMPP 即时通讯协议 SGIP 短信网关协议 这手机发短信 移动支付和网页支付 0x23232[0,1] 0x23232 0x23232 0x23232 只有协议,必 ...
- Html DOM 常用属性和方法
Node对象的节点类型***************************************************接口 nodeType常量 nodeType值 备注Element Node ...
- Cocos2d-x 3.0 beta 中加入附加项目,解决无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or directory”
Cocos2d-x 3.0 Alpha 1开始 对目录结构进行了整合.结果有些附加项目也被在项目中被精简出去. 比如说如果你需要使用CocoStdio导出的JSON.或使用Extensions扩展库, ...
- redis基本命令的演示:
import redis r = redis.Redis(host='127.0.0.1', port=6379,db = 0) #查看匹配redis的数据 r.keys() #查看redis的大小 ...
- python获取对象信息
获取对象信息 拿到一个变量,除了用 isinstance() 判断它是否是某种类型的实例外,还有没有别的方法获取到更多的信息呢? 例如,已有定义: class Person(object): def ...
- http://www.cnblogs.com/xdp-gacl/p/4040019.html
http://www.cnblogs.com/xdp-gacl/p/4040019.html
- Delphi XML-RPC 中文乱码解决方法
http://download.csdn.net/user/csm2432/uploads/2
- 【Xamarin挖墙脚系列:对设备/模拟器的查看调试监听】
原文:[Xamarin挖墙脚系列:对设备/模拟器的查看调试监听] 有时候我们需要查看模拟器中的文件,比如进行了文件IO操作,sqlite数据库的操作等.我们想查看内容,这时候,如何将内容导出来?由于A ...
- 《ruby编程语言》笔记 1
赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods i ...