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的更多相关文章

  1. gradle gradlew 的使用

    jcenter() 仓库比 mavenCentral() 仓库快,因此最好将jcenter 放前面,这样下载速度最快. 使用本地软件仓库:repositories { flatDir { dirs ' ...

  2. Device trees, Overlays and Parameters of Raspberry Pi

    Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default us ...

  3. [RxJS] Reusable multicasting with Subject factories

    The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusabl ...

  4. [rxjs] Shares a single subscription -- publish()

    If have an observable and you subscribe it twice, those tow subscritions have no connection. console ...

  5. rxjs5.X系列 —— Combination/Multicasting系列 api 笔记

    欢迎指导与讨论 :) 前言 本文是笔者翻译 RxJS 5.X 官网各类operation操作系列的的第三篇 -- Combination组合与Multicasting广播.如有错漏,希望大家指出提醒O ...

  6. Android 7.1 App Shortcuts使用

    Android 7.1 App Shortcuts使用 Android 7.1已经发了预览版, 这里是API Overview: API overview. 其中App Shortcuts是新提供的一 ...

  7. [译]RxJS 5.X基础篇

    欢迎指错与讨论 : ) 当前RxJS版本:5.0.0-beta.10.更详细的内容尽在RxJS官网http://reactivex.io/rxjs/manual/overview.html.文章比较长 ...

  8. angular2 学习笔记 ( rxjs 流 )

    RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅,  ...

  9. rxjs简单入门

    rxjs全名Reactive Extensions for JavaScript,Javascript的响应式扩展, 响应式的思路是把随时间不断变化的数据.状态.事件等等转成可被观察的序列(Obser ...

随机推荐

  1. Inversion of Control Containers and the Dependency Injection pattern--Martin Fowler

    原文地址:https://martinfowler.com/articles/injection.html n the Java community there's been a rush of li ...

  2. netty实现TCP长连接

    所用jar包 netty-all-4.1.30.Final.jar 密码:rzwe NettyConfig.java,存放连接的客户端 import io.netty.channel.group.Ch ...

  3. mkfs---创建Linux文件系统

    [root@xiaolizi ~]# mkfsmkfs mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs ...

  4. 小米开源文件管理器MiCodeFileExplorer-源码研究(6)-媒体文件MediaFile和文件类型MimeUtils

    接着之前的第4篇,本篇的2个类,仍然是工具类.MediaFile,媒体文件,定义了一大堆的常量,真正的有用的方法就几个.isAudioFileType.isVideoFileType之类的. Mime ...

  5. 使用注解的方式配置Servlet

    提到Servlet的配置,大多数人想到的应该都是在web.xml中配置吧.有没有更简洁的方式呢?今天就学到了採用注解的方式配置Servlet. 此方式尽管简便.但当然也存在问题. 採用注解的有点:你能 ...

  6. 小胖说事29-----iOS中Navigation中左滑pop页面的三种方法

    第三中类型.自己定义任何位置返回页面的方式,上边的类就是.m,大家能够贴过去使用.这个类是继承NavigationController的.用这个类初始化rootController就能够了.这里还有源 ...

  7. actionmode-ActionMode以及它的menu使用

    下图左边效果为Context Menu右边效果为ActionMode. ActionMode 其实就是替换在actionbar的位置上显示的一个控件.它跟actionbar一样,也是一种导航作用.只不 ...

  8. 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  9. ubuntu搭建交叉编译环境makeinfo: command not found

    解决办法:sudo apt-get install texinfo

  10. PYTHON学习第五天课后总结:

    今日重点: 数字类型 字符串类型 列表类型 元组类型 1,数字类型    数字类型为不可变类型   也只能将纯数字类型的字符串转换成int包括: 整型: int()     int() 为内置函数,可 ...