[RxJS] Multicast with a selector argument, as a sandbox

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(1000).take(6)
  .do(x => console.log(‘source ‘ + x))
  .map(x => Math.random());

var delayedResult = result.delay(500);
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(1000).take(6)
  .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(1000).take(6)
  .do(x => console.log(‘source ‘ + x))
  .map(x => Math.random())
  .multicast(subjectFactory, function sandbox(shared) {
    var delayedShare = shared.delay(500);
    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.

时间: 2024-12-22 08:41:56

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

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

一 take操作符 只发出源 Observable 最初发出的的N个值 (N = count). 如果源发出值的数量小于 count 的话,那么它的所有值都将发出.然后它便完成,无论源 Observable 是否完成. import { Component, OnInit } from '@angular/core'; import { range } from 'rxjs/observable/range'; import { take } from 'rxjs/operators/take'

Notification Centers 通知中心

Notification Centers 通知中心 A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria. The notification information is encapsulated in NSNotification objects. Client o

Cocos2d-x 3.1 Director ActionManger Scheduler初步分析

Director游戏主循环显示Node DisplayLinkDirector继承Director override了以下方法 virtual void mainLoop() override; virtual void setAnimationInterval(double value) override; virtual void startAnimation() override; virtual void stopAnimation() override; mainLoop()是游戏主循

18、Cocos2dx 3.0游戏开发找小三之cocos2d-x,请问你是怎么调度的咩

重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30478251 Cocos2d 的一大特色就是提供了事件驱动的游戏框架, 引擎会在合适的时候调用事件处理函数,我们只需要在函数中添加对各种游戏事件的处理, 就可以完成一个完整的游戏了. 例如,为了实现游戏的动态变化,Cocos2d 提供了两种定时器事件: 为了响应用户输入,Cocos2d 提供了触摸事件和传感器事件: 此外,Cocos2d 还提供了一系列

cocos2dx三种定时器的使用

 cocos2dx三种定时器的使用以及停止schedule,scheduleUpdate,scheduleOnce 今天白白跟大家分享一下cocos2dx中定时器的使用方法. 首先,什么是定时器呢?或许你有时候会想让某个函数不断的去执行,或许只是执行一次,获取你想让他每隔几秒执行一次,ok,这些都可以统统交给定时器来解决. cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce.了解其功能便会发现定时器真是太方便了,废话不多说,我们逐一学习一

Cocos2d-x 系列六之数据操作

一.定时器  在cocos2d-x中, 类似定时器的操作,不需要额外的写Timer,实际上,在Node元素中,已经添加了定时执行的功能: 先来看看在Node中的定义 // ... bool Node::isScheduled(SEL_SCHEDULE selector) { return _scheduler->isScheduled(selector, this); } void Node::scheduleUpdate() { scheduleUpdateWithPriority(0); }

cocos2dx三种定时器使用

 cocos2dx三种定时器的使用以及停止schedule.scheduleUpdate.scheduleOnce 今天白白跟大家分享一下cocos2dx中定时器的用法. 首先,什么是定时器呢?也许你有时候会想让某个函数不断的去运行.也许仅仅是运行一次,获取你想让他每隔几秒运行一次.ok.这些都能够统统交给定时器来解决. cocos2dx中有三种定时器:schedule,scheduleUpdate.scheduleOnce.了解其功能便会发现定时器真是太方便了,废话不多说,我们逐一学习一下

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

上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. 直入正题,我们打开CCScheduler.h文件看下里面都藏了些什么. 打开了CCScheduler.h 文件,还好,这个文件没有ccnode.h那么大有上午行,不然真的吐血了, 仅仅不到500行代码.这个文件里面一共有五个类的定义,老规矩,从加载的头文件开始阅读. #include <funct

Cocos2d-x 3.2:定时器的使用和原理探究(2)

Cocos2d-x 3.2:定时器的使用和原理探究(2) 本文转载至深入了解Cocos2d-x 3.x:定时器的使用和原理探究(2) 上一篇说到定时器的使用方法,这篇主要分析它的实现原理. 1.哈希链表 Cocos2d-x封装了一个结构体,叫做UT_hash_handle,只要在自定义的结构体中声明这个结构体变量,就实现了哈希链表,并且能使用一系列的哈希链表专用的宏.这个结构体的具体实现如下: 1 2 3 4 5 6 7 8 9 10 typedef struct UT_hash_handle