[RxJS] Multicasting shortcuts: publish() and variants

Because using multicast with a new Subject is such a common pattern, there is a shortcut in RxJS for this: the publish() operator. This lesson introduces publish() and its variants publishReplay(), publishBehavior(), publishLast(), share(), and shows how they simplify the creation of multicasted Observables.

var shared = Rx.Observable.interval(1000)
  .do(x => console.log(‘source ‘ + x))
  .multicast(new Rx.Subject())
  .refCount();

This partten multicast(new Rx.Subject()) to create sharable subject is so common, there is shortcut to doing this: ‘publish()‘:

var shared = Rx.Observable.interval(1000)
  .do(x => console.log(‘source ‘ + x))
  .publish()
  .refCount();

Also for BehaviourSubject(), AsyncSubject(), ReplaySubject() they all have:

// publish = multicast + Subject
// publishReplay = multicast + ReplaySubject
// publishBehavior = multicast + BehaviorSubject
// publishLast = multicast + AsyncSubject

In fact, publish.refCount() is also common used, so there is another alias for this =.=!!

var shared = Rx.Observable.interval(1000)
  .do(x => console.log(‘source ‘ + x))
  .share(); 

// share() == publish().refCount() == multiCast(new Rx.Subject()).refCount()
var shared = Rx.Observable.interval(1000)
  .do(x => console.log(‘source ‘ + x))
  .share();

// share = publish().refCount()
// publish = multicast + Subject
// publishReplay = multicast + ReplaySubject
// publishBehavior = multicast + BehaviorSubject
// publishLast = multicast + AsyncSubject

var observerA = {
  next: function (x) { console.log(‘A next ‘ + x); },
  error: function (err) { console.log(‘A error ‘ + err); },
  complete: function () { console.log(‘A done‘); },
};

var subA = shared.subscribe(observerA);

var observerB = {
  next: function (x) { console.log(‘B next ‘ + x); },
  error: function (err) { console.log(‘B error ‘ + err); },
  complete: function () { console.log(‘B done‘); },
};

var subB;
setTimeout(function () {
  subB = shared.subscribe(observerB);
}, 2000);

setTimeout(function () {
  subA.unsubscribe();
  console.log(‘unsubscribed A‘);
}, 5000);

setTimeout(function () {
  subB.unsubscribe();
  console.log(‘unsubscribed B‘);
}, 7000);
时间: 2024-10-09 13:57:16

[RxJS] Multicasting shortcuts: publish() and variants的相关文章

Android Tools Project Site

Android Tools Project Site Search this site   Projects Overview Screenshots Release Status Roadmap Download Preview Channel Recent Changes Technical docs New Build System Known Issues Tips Build Overview Contributing Feedback Technical docs‎ > ‎New B

[RxJS] Reusable multicasting with Subject factories

The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusable if the shared execution happens to complete or emit an error. In this lesson we will see how to use a simple Subject factory function in order to cr

[RxJS] Split an RxJS observable conditionally with windowToggle

There are variants of the window operator that allow you to split RxJS observables in different ways. In this lesson we will explore the windowToggle variant and see one of its use cases in user interfaces. Let's say we want to build a new functional

rxjs简单入门

rxjs全名Reactive Extensions for JavaScript,Javascript的响应式扩展, 响应式的思路是把随时间不断变化的数据.状态.事件等等转成可被观察的序列(Observable Sequence),然后订阅序列中那些Observable对象的变化,一旦变化,就会执行事先安排好的各种转换和操作 rxjs适用于异步场景,即前端交互中接口请求.浏览器事件以及自定义事件.通过使用rxjs带给我们前所未有的开发体验. 统一异步编程的规范,不管是Promise.ajax还是

RxJS入门(7)----创建一个完整的web application

在本章中,使用Rxjs,我们将创建一个典型的web application.我们将转化Dom Object Model(DOM)并且使用node.js中的websocket做c/s的交互. 用户界面部分,我们将使用RxJs-Domlibrary,这同样是Rxjs团队做的库,提供了方便的操作符来处理DOM和浏览器相关的使我们的编码更容易.服务器端:我们将是使用两个建立很好的node库,并用Observable封装他们的一些API到我们的application中. 在这一章之后,你将能使用RxJs创

RxJS 6有哪些新变化?

我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6.  rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的建议,逐步将原rxjs语法改为rxjs6后,要去掉rxjs-compact包. rxjs-compact包无法转换一些语法,我们的工程中没用到这些特性,原rxjs代码可以转为rxjs6. 原文链接:https://segmentfault.com/a/1190000014956260 RxJs 6于

[转]UiPath Keyboard Shortcuts

本文转自:https://docs.uipath.com/studio/docs/keyboard-shortcuts The complete list of keyboard shortcuts for UiPath Studio: File Management Ctrl + Shift + N - Creates a new Blank Process. Ctrl + O - Enables you to open a previously created workflow, eithe

RabbitMQ - Publish/Subscribe in Java

这次我们试试publish / subscribe模式, 也就是将一个消息发送给多个consumer. 这里用一个简单的小程序来说明publish / subscribe. 由一个provider提供消息,这个消息会被多个consumer接收. consumer对同一个消息做出不同的反应,比如打印.保存到文件.数据库什么的. 之前的例子可能会给人这种感觉: producer将消息发送到队列中,消息缓冲在队列中,consumer从队列获得消息. 但这并不正确. 在rabbit中,producer从

如何理解 RxJS?RxJS的中文API和使用教程

如何理解 RxJS? 我先附上RxJS的中文教程地址方便大家去了解和使用 中文使用教程:http://rxjs-china.org/_book/ 官方中文文档:https://buctwbzs.gitbooks.io/rxjs/content/ 好啦,我们开始讲一下如何理解它 RxJS 可能对很多人而言是一个从没听说过的新名词,那么 RxJS 到底是什么呢?本文中将予以简要介绍 在 Angular 2 中,我们遇到了一个新的概念 —— RxJS. 对很多人而言,这可能是一个比较难以理解的地方.所