Let's explore a different use of the multicast() operator in RxJS, where you can provide a selector function as a sandbox where the shared Observable is available.

When we have code like below:

var result = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.map(x => Math.random()); var delayedResult = result.delay();
var merged = result.merge(delayedResult); merged.subscribe( (x) => console.log(x))
/*
"source 0"
0.5832993222895915
"source 0"
0.031394357976560316
"source 1"
0.27602687148865
"source 1"
0.8762531748833942
"source 2"
0.49254272653868103
"source 2"
0.8024593359949526
...
*/

You can notice that, it runs 'result' block twice each time, it because 'merged' subscribe to 'result' and 'delayedResult' also subscribe to 'result', therefore it log out source twice.

If you only want one subscribe, you can use multicast(), with a second param which is sandbox function.

Normally you will use mulitcast() with refCount():

function subjectFactory() {
return new Rx.Subject();
} var result = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.map(x => Math.random())
.multicast(subjectFactory).refCount(); var sub = result.subscribe(x => console.log(x));

If you pass a second param:

var result = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.map(x => Math.random())
.multicast(subjectFactory, function sandbox(shared) {
var delayedShare = shared.delay();
var merged = shared.merge(delayedShare);
return merged;
}); result.subscribe(x => console.log(x));
/*
"source 0"
0.9214861149095479
0.9214861149095479
"source 1"
0.1684919218677523
0.1684919218677523
"source 2"
0.28182876689689795
0.28182876689689795
...
*/

Notice that, is you pass a second param to multicase(), the return value is no longer an connectableObservable. It is just a normal observable. So you cannot call 'refCount()' anymore.

And inside sandbox() function, you need to retrun a observable.

From the results can see, we no longer subscribe the source twice.

The takeaway is you should use a selector function in multicast when you want to create, let's say, a diamond-shaped dependency. Here we have a bifurcation. As you see we have shared, and it's used in two parts, and then we converge those two parts together to return one observable. That's kind of like a diamond shape, where we bifurcate, and then we converge.

That is one case where you almost always want to use a selector function in multicast. If you don't, then usually we use just multicast with a refCount. That's quite common to use.

[RxJS] Multicast with a selector argument, as a sandbox的更多相关文章

  1. RxJS v6 学习指南

    为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...

  2. rxjs的世界

    rxjs学习了几个月了,看了大量的东西,在理解Observable的本文借鉴的是渔夫的故事,原文,知识的主线以<深入浅出rxjs>为主,动图借鉴了rxjs中文社区翻译的文章和国外的一个动图 ...

  3. [RxJS] Conclusion: when to use Subjects

    As a conclusion to this course about RxJS subjects, let's review when and why should you use them. F ...

  4. RxJS之工具操作符 ( Angular环境 )

    一 delay操作符 源Observable延迟指定时间,再开始发射值. import { Component, OnInit } from '@angular/core'; import { of ...

  5. RxJS之转化操作符 ( Angular环境 )

    一 map操作符 类似于大家所熟知的 Array.prototype.map 方法,此操作符将投射函数应用于每个值 并且在输出 Observable 中发出投射后的结果. import { Compo ...

  6. RxJS之过滤操作符 ( Angular环境 )

    一 take操作符 只发出源 Observable 最初发出的的N个值 (N = count). 如果源发出值的数量小于 count 的话,那么它的所有值都将发出.然后它便完成,无论源 Observa ...

  7. RxJS之组合操作符 ( Angular环境 )

    一 merge操作符 把多个 Observables 的值混合到一个 Observable 中 import { Component, OnInit } from '@angular/core'; i ...

  8. Angular Multiple HTTP Requests with RxJS

    原文:https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs ----------------------------- ...

  9. Cocos2d-X3.0 刨根问底(六)----- 调度器Scheduler类源码分析

    上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. ...

随机推荐

  1. colrm---删除文件制定列

  2. Day6上午解题

    预计分数:100+100+30=230 实际分数:90+25+10=125 T1少判了一种情况,T2的贪心是错的,T3被卡了... T1 模拟水题,注意20的可以用3个5块的找 #include< ...

  3. 轻松学习Linux之本地安装系统

    1.安装Linux前的准备工作(详细讲解了系统分区,及类型) 2.轻松学习Linux之用光驱安装 3.轻松学习Linux之用光驱安装(之二) 4.硬盘安装Linux系统 本文出自 "李晨光原 ...

  4. selenium+python自动化处理时间控件

    尝试编写12306网站查询余票信息的自动化脚本时,碰到日期选择的问题,此处做一下记录:

  5. 用两个栈实现队列与用两个队列实现栈(Python实现)

    用两个栈实现队列: class QueueWithTwoStacks(object): def __init__(self): self._stack1 = [] self._stack2 = [] ...

  6. HDU 1848(sg博弈) Fibonacci again and again

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  7. 使用@Order调整配置类加载顺序

    转自:https://blog.csdn.net/qq_15037231/article/details/78158553 4.1 @Order Spring 4.2 利用@Order控制配置类的加载 ...

  8. 关于React中,map出来的元素添加事件问题

    用es6 map 的写法 直接绑定一个onTouchStart 事件不会报错. 用es5的map写法  如果不加上this  会报这个错误 无法读取未定义的属性 解决的方法是 绑定this  就可以了

  9. Django环境搭建(二)

    web框架 本质就是socket服务端 socket服务端:是计算机科学家在TCP/IP基础上进行封装,暴露出一个接口socket,就是一个收发数据的一个接口. 对于真实的web程序来说分为两部分:服 ...

  10. crm翻译导航栏

    在crm里面怎样翻译导航栏? 过程例如以下: 1 先新建一个解决方式.把网站地图加进去 2: 然后把这个解决方式到出来来,解压文件: 3:编辑第二个文件: watermark/2/text/aHR0c ...