[RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through
Understanding sources and subscribers makes it much easier to understand what's going on with mergeMap under the hood. Where a typical operator invokes destination.next directly, mergeMap wraps destination.next inside of a new source/subscriber combo so there's an "outer" next and an "inner" next.
import { fromEvent, of, Subscriber } from "rxjs"
import {
scan,
delay,
mergeMap
} from "rxjs/operators"
class MyMergeMapSubscriber extends Subscriber {
constructor(sub, fn) {
super(sub)
this.fn = fn
}
_next(value) {
console.log(`outer`, value)
const o$ = this.fn(value)
o$.subscribe({
next: value => {
console.log(` inner`, value)
this.destination.next(value)
}
})
}
}
const myMergeMap = fn => source =>
source.lift({
call(sub, source) {
source.subscribe(
new MyMergeMapSubscriber(sub, fn)
)
}
})
const observable$ = fromEvent(
document,
"click"
).pipe(
scan(i => i + , ),
myMergeMap(value => of(value).pipe(delay()))
)
const subscriber = {
next: value => {
console.log(value)
},
complete: () => {
console.log("done")
},
error: value => {
console.log(value)
}
}
observable$.subscribe(subscriber)
[RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through的更多相关文章
- [RxJS] Implement RxJS `switchMap` by Canceling Inner Subscriptions as Values are Passed Through
switchMap is mergeMap that checks for an "inner" subscription. If the "inner" su ...
- [RxJS] Implement RxJS `concatMap` by Waiting for Inner Subscriptions to Complete
Unlike mergeMap and switchMap, concatMap focuses on when "inner" subscriptions "compl ...
- [RxJS] Use RxJS mergeMap to map and merge high order observables
Like RxJS switchMap() is a shortcut for map() and switch(), we will see in this lesson how mergeMap( ...
- [RxJS] Convert RxJS Subjects to Observables
The use of RxJS Subjects is common, but not without problems. In this lesson we will see how they ca ...
- [RxJS] Use RxJS concatMap to map and concat high order observables
Like switchMap and mergeMap, concatMap is a shortcut for map() followed by a concatAll(). In this le ...
- [Javascript + rxjs] Simple drag and drop with Observables
Armed with the map and concatAll functions, we can create fairly complex interactions in a simple wa ...
- [RxJS] Implement the `map` Operator from Scratch in RxJS
While it's great to use the RxJS built-in operators, it's also important to realize you now have the ...
- [RxJS] Implement pause and resume feature correctly through RxJS
Eventually you will feel the need for pausing the observation of an Observable and resuming it later ...
- [RxJS] Chain RxJS Operators Together with a Custom `pipe` Function using Array.reduce
Instead of writing complex operators, it's usually best to write simple, single-purpose operators th ...
随机推荐
- Windows SubSystem for Linux(WSL)设置默认和设置默认登陆用户
使用wslconfig命令进行管理 1. 设置默认运行的linux系统 wslconfig /setdefault <DistributionName> 正如上面所说,如果执行wslco ...
- js模块化入门与commonjs解析与应用
JS模块化的基本原理 commonjs规范 commonjs在前端模块化中的基本使用 AMD与CMD规范剖析博客链接 一.JS模块化基本原理 在JS没有提出来模块化的时候,开发JS项目比较简单,同时也 ...
- (待解决)IDEA配置JDBC查询数据库PreparedStatement pstmt = dbconn.prepareStatement(sql)出现空指针错误
package com.demo; import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.*; i ...
- 【开发工具安装配置】MyEclipse,Tomcat,Mysql安装配置
配置步骤 注:以下路径仅供参考! 一.MyEclipse10 1. 1 破解版破解说明: (1)下载安装好Myeclipse,先不要运行. (2)打开破解工具目录下的cracker.jar文件或run ...
- webpack 之 plugin
loader和plugins的区别是:前者处理单个文件,而后者作用于整个打包过程 内置插件:BannerPlugin 可以在打包后的文件上方添加备注信息,如版权说明等 // webpack.confi ...
- vue 指令---气泡提示(手撸实战)
菜鸟学习之路//L6zt github 自己在造组件轮子,也就是瞎搞.自己写了个slider组件,想加个气泡提示.为了复用和省事特此写了个指令来解决.预览地址项目地址 github 我叫给它胡博 cs ...
- Verilog仿真事件队列
1.分层的事件队列 2.执行事件的队列 3.仿真时间的计算 4.同一层事件,无先后顺序 这个点:觉得Verilog与systemVerilog比较,Verilog比较笼统,systemVerilog则 ...
- Java学习之理解递归
Java支持递归.递归是根据自身定义内容的过程.就Java编程而言,递归是一个允许方法调用自身的特性.调用自身的方法被称为递归.典型的例子就是阶乘的计算,N的阶乘就是从1到N之间所有整数的乘积. 当方 ...
- UVALive 2957 Bring Them There
Bring Them There Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. O ...
- 【数据传输 2】批量导入的前奏:将Excel表中的数据转换为DataTable类型
导读:我们知道,在数据库中,数据集DataSet是由多张DataTable表组成.所以,如果我们需要将数据从外部导入到数据库中,那么要做的很重要的一步是将这些数据转换为数据库可以接受的结构.今天在用S ...