[RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS
When subscribers create new "inner" sources and subscriptions, you run the risk of losing control of them when the outer subscriber unsubscribes. This is handled easily enough if you use the add method from the Subscriber class to add the "inner" subscription to the Subscriber.
class MyConcatMapSubscriber extends Subscriber {
innerSubscription
buffer = []
constructor(sub, fn) {
super(sub)
this.fn = fn
}
_next(value) {
const { isStopped } = this.innerSubscription || {
isStopped: true
}
if (!isStopped) {
this.buffer = [...this.buffer, value]
} else {
const o$ = this.fn(value)
this.innerSubscription = o$.subscribe({
next: value => {
this.destination.next(value)
},
complete: () => {
console.log(this.buffer)
if (this.buffer.length) {
const [first, ...rest] = this.buffer
this.buffer = rest
this._next(first)
}
}
})
// to tell when the outter subscription complete, the inner subscription should also completed
this.add(this.innerSubscription)
}
}
}
[RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS的更多相关文章
- [RxJS] Add debug method to Observable in TypeScript
Observable.prototype.debug = function(message: any) { return this.do( (next) => { if(!environment ...
- RxJS速成 (下)
上一部分: http://www.cnblogs.com/cgzl/p/8641738.html Subject Subject比较特殊, 它即是Observable又是Observer. 作为Obs ...
- RxJS -- Subscription
Subscription是什么? 当subscribe一个observable的时候, 返回的就是一个subscription. 它是一个一次性对象(disposable), 它有一个非常重要的方法 ...
- rxjs 入门--环境配置
原文: https://codingthesmartway.com/getting-started-with-rxjs-part-1-setting-up-the-development-enviro ...
- rxjs与vue
原创文章,转载请注明出处 使用vue-rx插件将vue和rxjs联系起来 在main.js中将vue-rx注入vue中 import Vue from 'vue' import App from '. ...
- [译]RxJS 5.X基础篇
欢迎指错与讨论 : ) 当前RxJS版本:5.0.0-beta.10.更详细的内容尽在RxJS官网http://reactivex.io/rxjs/manual/overview.html.文章比较长 ...
- [RxJS + AngularJS] Sync Requests with RxJS and Angular
When you implement a search bar, the user can make several different queries in a row. With a Promis ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
- [Angular 2] Managing State in RxJS with StartWith and Scan
The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves ...
随机推荐
- dockerfile note
dockerfile note reference summary defination docker can build images automatically by reading the in ...
- C++变量和基本类型
1. 如何选择类型的准则 当明确知晓数值不可能为负的时候,应该选择无符号类型. 使用int执行整数运算的时候,在实际应用中,short常常显得太小而long一般和int有一样的尺寸,如果数值超过了in ...
- git命令(使用visual studio)
拉取,提取,合并 提交到本地 切换分支 创建分支 推送到远端 删除本地分支 删除远程分支
- Node.js 创建server服务器
var http=require('http'); //引入http模块 var server=http.createServer(function(req,res){ //创建一个server r ...
- 内存区--Java
一.概述 对于 Java 程序员来说,在虚拟机自动内存管理机制下,不再需要像C/C++程序开发程序员这样为内一个 new 操作去写对应的 delete/free 操作,不容易出现内存泄漏和内存溢出问题 ...
- redis:安装配置主从
1.安装依赖包 yum install gcc gcc-c++ -y 2.下载安装包,解压 cd /usr/local/src/wget http://download.redis.io/releas ...
- validate 常用的输入框校验
记录一下angular可以直接用的输入框校验器,外加一个国内手机号码的校验 <!DOCTYPE html> <html> <head> <meta chars ...
- maven构建springmvc项目
1.Eclipse中 NEW ->OTHER->Maven->maven project 2.选择项目路径 3.选择项目类型->next->输入groupid和artif ...
- Canvas标签
1.Canvas标签: HTML5<canvas>元素用于图形的绘制,通过脚本(通常是javascript)来完成<canvas>标签只是图形容器,必须使用脚本来绘制图形.你可 ...
- python 简易计算器(只能计算加减乘除和括号)
import re # 格式化字符串函数(消除一些错误的格式) def format_string(string): # 一系列的替换语句 string = string.replace(" ...