[RxJS] Multicasting shortcuts: publish() and variants
Because using multicast with a new Subject is such a common pattern, there is a shortcut in RxJS for this: the publish() operator. This lesson introduces publish() and its variants publishReplay(), publishBehavior(), publishLast(), share(), and shows how they simplify the creation of multicasted Observables.
var shared = Rx.Observable.interval()
.do(x => console.log('source ' + x))
.multicast(new Rx.Subject())
.refCount();
This partten multicast(new Rx.Subject()) to create sharable subject is so common, there is shortcut to doing this: 'publish()':
var shared = Rx.Observable.interval()
.do(x => console.log('source ' + x))
.publish()
.refCount();
Also for BehaviourSubject(), AsyncSubject(), ReplaySubject() they all have:
// publish = multicast + Subject
// publishReplay = multicast + ReplaySubject
// publishBehavior = multicast + BehaviorSubject
// publishLast = multicast + AsyncSubject
In fact, publish.refCount() is also common used, so there is another alias for this =.=!!
var shared = Rx.Observable.interval()
.do(x => console.log('source ' + x))
.share(); // share() == publish().refCount() == multiCast(new Rx.Subject()).refCount()
var shared = Rx.Observable.interval()
.do(x => console.log('source ' + x))
.share(); // share = publish().refCount()
// publish = multicast + Subject
// publishReplay = multicast + ReplaySubject
// publishBehavior = multicast + BehaviorSubject
// publishLast = multicast + AsyncSubject var observerA = {
next: function (x) { console.log('A next ' + x); },
error: function (err) { console.log('A error ' + err); },
complete: function () { console.log('A done'); },
}; var subA = shared.subscribe(observerA); var observerB = {
next: function (x) { console.log('B next ' + x); },
error: function (err) { console.log('B error ' + err); },
complete: function () { console.log('B done'); },
}; var subB;
setTimeout(function () {
subB = shared.subscribe(observerB);
}, ); setTimeout(function () {
subA.unsubscribe();
console.log('unsubscribed A');
}, ); setTimeout(function () {
subB.unsubscribe();
console.log('unsubscribed B');
}, );
[RxJS] Multicasting shortcuts: publish() and variants的更多相关文章
- gradle gradlew 的使用
		jcenter() 仓库比 mavenCentral() 仓库快,因此最好将jcenter 放前面,这样下载速度最快. 使用本地软件仓库:repositories { flatDir { dirs ' ... 
- Device trees, Overlays and Parameters of Raspberry Pi
		Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default us ... 
- [RxJS] Reusable multicasting with Subject factories
		The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusabl ... 
- [rxjs] Shares a single subscription -- publish()
		If have an observable and you subscribe it twice, those tow subscritions have no connection. console ... 
- rxjs5.X系列 —— Combination/Multicasting系列 api 笔记
		欢迎指导与讨论 :) 前言 本文是笔者翻译 RxJS 5.X 官网各类operation操作系列的的第三篇 -- Combination组合与Multicasting广播.如有错漏,希望大家指出提醒O ... 
- Android 7.1 App Shortcuts使用
		Android 7.1 App Shortcuts使用 Android 7.1已经发了预览版, 这里是API Overview: API overview. 其中App Shortcuts是新提供的一 ... 
- [译]RxJS 5.X基础篇
		欢迎指错与讨论 : ) 当前RxJS版本:5.0.0-beta.10.更详细的内容尽在RxJS官网http://reactivex.io/rxjs/manual/overview.html.文章比较长 ... 
- angular2 学习笔记 ( rxjs 流 )
		RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ... 
- rxjs简单入门
		rxjs全名Reactive Extensions for JavaScript,Javascript的响应式扩展, 响应式的思路是把随时间不断变化的数据.状态.事件等等转成可被观察的序列(Obser ... 
随机推荐
- 4.Maven之(四)Maven命令
			转自:https://blog.csdn.net/u012152619/article/details/51473410 
- Linux下截图技巧
			在需要Linux显示图片的场合,最普通的方法,会考虑用数码相,或是用Vmware,或VPc来抓拍,这样以来会比较麻烦,Linux也自带了些工具例如Gimp,ksnapshot这里我介绍一 ... 
- ES6第二节:新的声明方式
			通过上一节的环境搭建完成,接下来我们就可以愉快的探索ES6的新世界了!下面我们从新的声明方式开始: 在ES6里新加了两种声明方式:let 和 const,以前我们都是用var去作声明,接下来我们一一比 ... 
- 网络流最大流dinic的板子
			void add(int u,int v,int w){ e[tot].v=v; e[tot].w=w; e[tot].nt=pre[u]; pre[u]=tot++; e[tot].v=u; e[t ... 
- R 语言下常用第三方库的说明
			1. doBy 官方文档见 CRAN - Package doBy doBy 主要适用于以下操作: 1) Facilities for groupwise computations of summar ... 
- java三元表达式编程规范问题
			package day01; public class Program { public static void main(String[] args) { // TODO Auto-g ... 
- Objective-C基础笔记(7)Foundation中的常用结构体
			一.NSRange NSRange的定义: typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; N ... 
- 【hdu 1533】Going Home
			[链接]http://acm.hdu.edu.cn/showproblem.php?pid=1533 [题意] 一个N*M地图上有相同数量的字符H和字符m,m代表一个 人,H代表一个房子.人到房子的花 ... 
- 【iOS开发-29】解决方式:TabBar的图片不显示,仅仅显示灰色的正方形
			(1)现象 tabbar上的图片变成一块正方形的灰色块块,原先的图片没有了. (2)原因 tabbar上的图片本质上不是一个图片.而是一个形状图片.系统对我们使用的图片也仅仅是把当中的形状" ... 
- 报错 关于python的x和y不等长
			ValueError: shape mismatch: objects cannot be broadcast to a single shape 这个错误可能是因为传入的两个参数数据长度不一样,比如 ... 
