博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RxJS] Connection operator: multicast and connect
阅读量:6791 次
发布时间:2019-06-26

本文共 3144 字,大约阅读时间需要 10 分钟。

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"*/

 

转载地址:http://kgago.baihongyu.com/

你可能感兴趣的文章
20种 IT 职业明年将大幅涨薪,无线网络工程师最高
查看>>
《C语言编程——零基础初学者指南(第3版)》一第2章 编写第一个C程序2.1 概述...
查看>>
《HTML5+CSS3网页设计入门必读》——1.3 理解Web内容递送
查看>>
oracle table-lock的5种模式
查看>>
《 线性代数及其应用 (原书第4版)》——2.8 R^n的子空间
查看>>
初创公司如何快速低耗实现数据化运营
查看>>
《循序渐进学Docker》——导读
查看>>
《树莓派开发实战(第2版)》——1.8 使用复合视频显示器/TV
查看>>
编码之道:取个好名字很重要
查看>>
《树莓派开发实战(第2版)》——1.5 通过NOOBS刷写microSD卡
查看>>
《Python Cookbook(第3版)中文版》——1.7 让字典保持有序
查看>>
在 Linux 中设置 sudo 的十条 sudoers 实用配置
查看>>
Linux 有问必答:如何在 Linux 中永久修改 USB 设备权限
查看>>
《第三方JavaScript编程》——7.2 跨站脚本
查看>>
《师兄教你找工作——100场面试 20个offer背后的求职秘密》一导读
查看>>
为PetaPoco添加Fill方法
查看>>
哈哈,找到一种方式来简单模拟EXTJS中与服务器的AJAX交互啦。
查看>>
[WinForm]DataGridView列头右键菜单
查看>>
swing中定时启动的实现
查看>>
Spring IO Platform
查看>>