[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 ...
随机推荐
- 漫漫人生路,学点Jakarta基础-Java8函数式编程
接口默认方法 Java8版本以后新增了接口的默认方法,不仅仅只能包含抽象方法,接口也可以包含若干个实例方法.在接口内定义实例方法(但是注意需要使用default关键字) 在此定义的方法并非抽象方法,而 ...
- 关于exports 和 module.exports
本文来源为node.js社区附上链接 http://cnodejs.org/topic/5231a630101e574521e45ef8 require 用来加载代码,而 exports 和 modu ...
- Kinect 开发 —— 录音
不涉及语音识别~~ <Window x:Class="KinectRecordAudio.MainWindow" xmlns="http://schemas.mic ...
- 【CS Round #39 (Div. 2 only) B】Circle Elimination
[Link]:https://csacademy.com/contest/round-39/task/circle-elimination/ [Description] [Solution] 把n个点 ...
- DOM和SAX是应用中操纵XML文档的差别
查看原文:http://www.ibloger.net/article/205.html DOM和SAX是应用中操纵XML文档的两种主要API.它们分别解释例如以下: DOM.即Do ...
- 高速数论变换(NTT)
今天的A题.裸的ntt,但我不会,于是白送了50分. 于是跑来学一下ntt. 题面非常easy.就懒得贴了,那不是我要说的重点. 重点是NTT,也称高速数论变换. 在非常多问题中,我们可能会遇到在模意 ...
- Mahout应用
不多说,直接上干货! Mahout作为Apache基金会的顶级项目之一,Mahout的应用也极其广泛,一般分为商业应用和学术应用. 在商业应用中,Adobe AMP公司使用Mahout的聚类算法把用户 ...
- C# 异步延时执行
https://blog.csdn.net/xiawu1990/article/details/78350253?utm_source=blogxgwz7 var t = Task.Run(async ...
- BZOJ1014[JSOI2008]火星人prefix(splay维护hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- C++访问WebService gSoap方式
一. gSOAP访问WebService 1. 下载gSOAP gSOAP 2.7.17 版下载地址http://sourceforge.net/projects/g ...