[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 create a new Subject, one for each shared execution, whenever connect() is called.
var shared = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.multicast(new Rx.Subject())
.refCount();
The code above, after subject emit 0,1,2, three values, then it completes. It means if you want to subscribe the subject again, it won't emit anything because it is completed.
If you want to reuse the 'shared' subject even after subject complete, you need to use subject factories, which simply just a function return new Subject():
function subjectFactory() {
return new Rx.Subject();
} var shared = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.multicast(subjectFactory)
.refCount();
So now even you resubscribe after subject complete, it will emit you new value.
function subjectFactory() {
return new Rx.Subject();
} var shared = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.multicast(subjectFactory)
.refCount(); // subject: --0--1--2--3--4--5|
// A
// subject2: --0--1--2--3--4--5| 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); // 0 => 1
console.log('subscribed A'); 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);
console.log('subscribed B');
}, ); setTimeout(function () {
subA.unsubscribe();
console.log('unsubscribed A');
}, ); setTimeout(function () {
subB.unsubscribe();
console.log('unsubscribed B');
}, ); setTimeout(function () {
subA = shared.subscribe(observerA); // 0 => 1 (connect)
console.log('subscribed A');
}, );
/**
"subscribed A"
"source 0"
"A next 0"
"source 1"
"A next 1"
"subscribed B"
"source 2"
"A next 2"
"B next 2"
"A done"
"B done"
"unsubscribed A"
"unsubscribed B"
"subscribed A"
"source 0"
"A next 0"
"source 1"
"A next 1"
"source 2"
"A next 2"
"A done" */
[RxJS] Reusable multicasting with Subject factories的更多相关文章
- RxJS学习笔记之Subject
本文为原创文章,转载请标明出处 目录 Subject BehaviorSubject ReplaySubject AsyncSubject 1. Subject 总的来说,Subject 既是能够将值 ...
- RxJS - Subject(转)
Observer Pattern 观察者模式定义 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态 ...
- 【Rxjs】 - 解析四种主题Subject
原文地址: https://segmentfault.com/a/1190000012669794 引言 开发ngx(angular 2+)应用时,基本上到处都会用到rxjs来处理异步请求,事件调用等 ...
- 使用 Rx 中预定义的 Subject
看到一幅有趣的关于 Rx 学习的图,想知道学习 Rx 的学习曲线?不,是峭壁! 我们可以直接通过 Rx 的 Observer 来创建 Observable 对象. 但是,使用这种方式往往比较复杂,在特 ...
- RxJS 6有哪些新变化?
我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6. rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的 ...
- Angular 2 技能图谱skill-map
# Angular 2 技能图谱 ## 模块 ### 自定义模块 - 根模块 - 特性模块 - 共享模块 - 核心模块 ### 内置模块 - ApplicationModule 模块 - Common ...
- RXJS Observable的冷,热和Subject
一.Observable的冷和热 Observable 热:直播.所有的观察者,无论进来的早还是晚,看到的是同样内容的同样进度,订阅的时候得到的都是最新时刻发送的值. Observable 冷:点播. ...
- RxJS之Subject主题 ( Angular环境 )
一 Subject主题 Subject是Observable的子类.- Subject是多播的,允许将值多播给多个观察者.普通的 Observable 是单播的. 在 Subject 的内部,subs ...
- import { Subject } from 'rxjs/Subject';
shared-service.ts import { Observable } from 'rxjs/Observable'; import { Injectable } from '@angular ...
随机推荐
- 解决root登录 -bash-4.2# 的问题
- 平衡数之Treap
#include <memory>//智能指针头文件 #include <random>//随机数头文件 #include <iostream> #include ...
- [RxJS] Marbles Testings
Install: npm install — save-dev jasmine-marbles Basic example: import {cold, getTestScheduler} from ...
- TextView-显示自己添加的字体样式
1.首先要把我们的字体放到相应的目录下 如果我们仅仅是想要验证一个字体,我们可以直接 我们的字体push到 手机 /system/fonts/ 目录下面 2.在代码中进行设置 import andro ...
- CF-833B The Bakery(线段树优化Dp)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredie ...
- STL中erase()的陷阱
最近在刷stl源码剖析这本书时,对于vector的erase()函数引起了我的注意 在删除单个元素时是这样定义的: iterator erase(iterator position){ !=end() ...
- WPF 入门《数据绑定》
简单而言, 数据绑定是一种关系, 这种关系告诉WPF 从一个源目标对象中提取一些信息, 并且使用该信息设置为目标对象的属性.目标属性总是依赖项属性, 并且通常位于WPF元素中. 然而, 源对象可以是任 ...
- Android内存泄露分析之StrictMode
转载请注明地址:http://blog.csdn.NET/yincheng886337/article/details/50524709 StrictMode(严格模式)使用 StrictMode严格 ...
- AVCaptureSession音频视频采集
// // AudioVideoCaptureViewController.m // live // // Created by lujunjie on 2016/10/31. // Copyrigh ...
- Hypervisor, computer system, and virtual processor scheduling method
A hypervisor calculates the total number of processor cycles (the number of processor cycles of one ...