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. Scott Hanselman的问题-3

    .Net程序员面试 中级篇 (回答Scott Hanselman的问题)   继<.Net 程序员面试 C# 语言篇 (回答Scott Hanselman的问题)>跟<.Net程序员 ...

  2. java(异常体系及权限修饰符)

    java异常体系 异常的体系: 异常体系: --------| Throwable 所有错误或者异常的父类 --------------| Error(错误) --------------| Exce ...

  3. PHP foreach遍历数组之如何判断当前值已经是数组的最后一个

    先给出foreach的两种语法格式 1,foreach (array_expression as $value) statement 2,foreach (array_expression as $k ...

  4. 如何在canvas中画出一个太极图

    先放一个效果图: 代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /&g ...

  5. 洛谷——P1307 数字反转

    https://www.luogu.org/problem/show?pid=1307#sub 题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原 ...

  6. hdoj 2122 Ice_cream’s world III【最小生成树】

    Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. Vue的学习--遇到的一些问题和解决方法

    包括: 1.Missing space before function parentheses 2.如何给.vue文件的页面添加css 3.如何给.vue文件页面里的元素添加监听器 4.如何为每一个页 ...

  8. Vue render: h => h(App) $mount

    $mount()手动挂载 当Vue实例没有el属性时,则该实例尚没有挂载到某个dom中: 假如需要延迟挂载,可以在之后手动调用vm.$mount()方法来挂载.例如: new Vue({ //el: ...

  9. 手动挂接NFS

     环境: 单板:s3c2440 内核:Linux-2.6.22.6 U-boot1.16 初始根文件系统Yaffs2 前提条件 1. 开发板上要烧写好文件系统 2. 能正常开机进入Linux系统 3. ...

  10. tomcat做成windows服务之后使用JMX监控的问题

    转载:http://blog.chinaunix.net/uid-20449851-id-2369842.html