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. java(运算符,控制流程语句,函数 )

    运算符 数据类型转换: 小数据类型-------->大数据类型(自动类型转换) 大数据类型--------->小数据类型(强制类型转换) 强制类型转换的格式: 小数据类型  变量名 = ( ...

  2. 优化HTML代码的多种技巧

    怎样提升Web页面的性能,非常多开发者从多个方面来下手如JavaScript.图像优化.server配置,文件压缩或是调整CSS. 非常显然HTML 已经达到了一个瓶颈.虽然它是开发Web 界面必备的 ...

  3. OpenCASCADE Job - 深圳鞋博士

    鞋博士 鞋博士经过8年沉淀,在鞋类工业4.0全流程平台上积累了相当的技术实力,获投资商亲睐. 新的一年,在投资商协助下,将踏上新的征途,因此诚邀您加盟顶层技术合伙人. 如果您具备以下实力,我们期待您的 ...

  4. SQlite-数据库的访问实例(转)

    1.DBAdapter类: 1 package com.cnzcom.android.quickdial; import android.content.ContentValues; import a ...

  5. 升级你的Linux日志系统

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  6. Mysql数据库的瓶颈处理一点建议

    Mysql数据库的瓶颈处理一点建议         我们在使用Mysql数据库是常见的两个瓶颈是CPU和I/O的瓶颈,CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据时候.磁盘I/O瓶颈的出 ...

  7. Vue的全选功能实现思路

    全选功能的实现主要分两步: 1. 点击全选框选中所有选择框. 2. 当所有选择框都被选中时,勾选全选框. 详细思路: 1. 点击全选框选中所有选择框: 给全选框绑定一个值,然后添加change时间,当 ...

  8. @property 和@synthesize

    xcode4.4之后,@property包括了@synthesize的功能. 这是编译器的升级. @property有几个作用:1)默认生成一个私有成员变量,并有一个带下划线的别名如_age   2) ...

  9. x264代码剖析(三):主函数main()、解析函数parse()与编码函数encode()

    x264代码剖析(三):主函数main().解析函数parse()与编码函数encode() x264的入口函数为main().main()函数首先调用parse()解析输入的參数,然后调用encod ...

  10. cx_Oracle

    cx_Oracle 安装 pip install cx_Oracle 只是我没用那个安装成功过.我找了rpm 包. http://nchc.dl.sourceforge.net/project/cx- ...