We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple observers. However, this technique requires some laborious setting up. In this lesson we will learn about the multicast() operator which helps solve the same problem with less code, and with a neater API.

Let's go back and remember why did we need subjects in the first place? Originally, we had one typical observable, but we wanted two observers A and B, to see the same execution of that observable.

Does that mean that every time that we want to have multiple observers we need to set up a subject, and subscribe to the observables, subscribe to the subjects?

This system is not so ergonomic to set up. That's why there exists an operator or a method that simplifies all of this for us. That would be multicastmulticastis an operator on a normal observable. It takes here an argument, which is a subject.

// var source = Rx.Observable
// .interval(100)
// .take(5);
// var subject = new Rx.Subject();
// source.subscribe(subject);
var connectableObservable = Rx.Observable
.interval(100)
.take(5)
.multicast(new Rx.Subject());
connectableObservable.connect(); var observerA = {
next: function (x) { console.log('A next ' + x); },
error: function (err) { console.log('A error ' + err); },
complete: function () { console.log('A done'); },
}; connectableObservable.subscribe(observerA);
console.log('observerA subscribed'); var observerB = {
next: function (x) { console.log('B next ' + x); },
error: function (err) { console.log('B error ' + err); },
complete: function () { console.log('B done'); },
}; setTimeout(function () {
connectableObservable.subscribe(observerB);
console.log('observerB subscribed');
}, 300);

Now when we connect this observable, this connectableObservable, it will use a ReplaySubject to subscribe to this observable. That means that when the late observer arrives here, it will see the last values replayed to it. If we run this B arrives late, but B sees the latest values, zero and one, for instance.

// var source = Rx.Observable
// .interval(100)
// .take(5);
// var subject = new Rx.Subject();
// source.subscribe(subject);
var connectableObservable = Rx.Observable
.interval(100)
.take(5)
.multicast(new Rx.ReplaySubject(100));
connectableObservable.connect(); var observerA = {
next: function (x) { console.log('A next ' + x); },
error: function (err) { console.log('A error ' + err); },
complete: function () { console.log('A done'); },
}; connectableObservable.subscribe(observerA);
console.log('observerA subscribed'); var observerB = {
next: function (x) { console.log('B next ' + x); },
error: function (err) { console.log('B error ' + err); },
complete: function () { console.log('B done'); },
}; setTimeout(function () {
console.log('observerB subscribed');
connectableObservable.subscribe(observerB);
}, 300);
/*"observerA subscribed"
"A next 0"
"A next 1"
"A next 2"
"observerB subscribed"
"B next 0"
"B next 1"
"B next 2"
"A next 3"
"B next 3"
"A next 4"
"B next 4"
"A done"
"B done"*/

[RxJS] Connection operator: multicast and connect的更多相关文章

  1. rxjs自定义operator

    rxjs自定义operator

  2. [RxJS] Creation operator: of()

    RxJS is a lot about the so-called "operators". We will learn most of the important operato ...

  3. [RxJS] Transformation operator: repeat

    Operator repeat() is somewhat similar to retry(), but is not for handling operators. In this lesson ...

  4. [RxJS] Transformation operator: buffer, bufferCount, bufferTime

    This lesson will teach you about another horizontal combination operator: buffer and its variants. B ...

  5. [RxJS] Transformation operator: scan

    All of the combination operators take two or more observables as input. These operators may also be ...

  6. [RxJS] Combination operator: withLatestFrom

    Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...

  7. [RxJS] Combination operator: combineLatest

    While merge is an OR-style combination operator, combineLatest is an AND-style combination operator. ...

  8. [RxJS] Filtering operator: filter

    This lesson introduces filter: an operator that allows us to let only certain events pass, while ign ...

  9. [RxJS] Utility operator: do

    We just saw map which is a transformation operator. There are a couple of categories of operators, s ...

随机推荐

  1. Android 将HTML5封装成android应用APK文件的几种方法

    越来越多的开发者热衷于使用html5+JavaScript开发移动Web App.不过,HTML5 Web APP的出现能否在未来取代移动应用,就目前来说,还是个未知数.一方面,用户在使用习惯上,不喜 ...

  2. [BZOJ4555 TJOI2016 HEOI2016 求和]

    ​ 第一篇博客,请大家多多关照.(鞠躬 BZOJ4555 TJOI2016 HEOI2016 求和 题意: ​ 给定一个正整数\(n\)(\(1\leqq n \leqq100000\)),求: \[ ...

  3. ip---查看网络信息

    Linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者. ifconfig属于net-tools.ip属于iproute2 设置一个IP地址,可以使用下列ip命令: ip add ...

  4. docker 部署 jenkins server

    1. 拉取一个jenkins 镜像 docker pull jenkins 2. 创建与jenkins配置目录对应的,容器外的,文件目录,并修改相应的权限 mkdir /home/jenkins ch ...

  5. 【Good Bye 2017 C】 New Year and Curling

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举前i-1个圆. 哪些圆和它相交. 取圆心纵坐标最大的那个圆就可以了. [代码] #include <bits/stdc++ ...

  6. 【Uva 11080】Place the Guards

    [Link]: [Description] 一些城市,之间有道路相连,现在要安放警卫,警卫能看守到当前点周围的边,一条边只能有一个警卫看守,问是否有方案,如果有最少放几个警卫. [Solution] ...

  7. 浅谈Git与SVN的使用感受

    作为版本号控制工作.两者的做大的差别应该在于:Git属于分布式版本号控制工具,而SVN属于集中式的版本号控制工具.分布式的优点是什么呢?举个样例来说.当你在火车上离线状态下编程工作,在某个阶段会须要先 ...

  8. asp.net Code学习一(vs code跨平台软件操作)

    1.命令行: dotnet new -t web 创建web项目 dotnet new restore build pubilsh run test pack dotnet -info / -h do ...

  9. thinkphp 整合 swiftmailer 实现邮件发送

    thinkphp swiftmailer(phpmailer) 文件夹结构 图 1 swiftmailer-phpmailer 将swiftmailer整合到thinkphp中.如上图 1 我下载的版 ...

  10. POJ--1753--Flip Game【DFS】

    链接:http://poj.org/problem? id=1753 题意:一个4*4的方格,有白棋或者黑棋.每次操作是一个位置的颜色翻转,即白变黑.黑变白,而且与它相邻的四个位置的颜色也都跟着改变, ...