当前位置:网站首页>Rxjs mergeMap 的使用场合
Rxjs mergeMap 的使用场合
2022-06-27 16:18:00 【汪子熙】
注意:
- flatMap 是 mergeMap 的别名。
- 如果一次只能激活一个内部订阅,请使用 switchMap.
- 如果内部 observables 的发射和订阅顺序很重要,请使用 concatMap.
当需要展平内部 observable 但想要手动控制内部订阅的数量时,是 mergeMap 极佳的使用场合。
例如,当使用 switchMap 时,每个内部订阅在源发出时完成,即任意时间段只允许一个活动的内部订阅。相比之下,mergeMap 允许同时激活多个内部订阅。因此,mergeMap 最常见的用例之一是不应取消的请求,这些请求被认为是写入而不是读取。
一个典型的例子就是 SAP 电商云购物车里不同行项目的金额,可以并行地进行增减操作。
请注意,如果这些写入必须保持顺序,则 concatMap 是更好的选择。比如数据库的写操作。
由于 mergeMap 一次维护多个活动的内部订阅,因此可能会由于长期存在的内部订阅造成内存泄漏。一个基本的例子是,如果使用内部计时器或 dom 事件流映射到可观察对象。在这些情况下,如果仍然希望使用 mergeMap,一个好办法是利用另一个运算符来管理内部订阅的完成,比如考虑 take 或 takeUntil。当然还可以使用 concurrent 参数限制一次活动内部订阅的数量。
看一个例子:
import {
fromEvent, of } from 'rxjs';
import {
mergeMap, delay } from 'rxjs/operators';
// faking network request for save
const saveLocation = location => {
return of(location).pipe(delay(500));
};
// streams
const click$ = fromEvent(document, 'click');
click$
.pipe(
mergeMap((e: MouseEvent) => {
return saveLocation({
x: e.clientX,
y: e.clientY,
timestamp: Date.now()
});
})
)
// Saved! {x: 98, y: 170, ...}
.subscribe(r => console.log('Saved!', r));
saveLocation 是一个函数,它能把任何传入的输入参数,包裹成一个 Observable,并且这个 Observable 并不是立即 emit 数据,而是延迟 500 毫秒。

mergeMap 接收一个函数作为输入参数,这个函数的输入参数就是通过 pipe 链接 mergeMap 的 Observable 里包含的元素,即 MouseEvent;project 返回的数据类型是一个新的 Observable,包含了屏幕点击的 X 和 Y 坐标以及当前时间戳。

最后产生的输出:

边栏推荐
- Shardingsphere & Atlas & MYCAT comparison
- VSCode 建议你启用 gopls,它到底是个什么东东?
- [UVM basics] set a monitor at the input port of the DUT to explain the necessity
- PostgreSQL数据库WAL——资源管理器RMGR
- Two methods of MySQL database login and logout
- Application of tdengine in monitoring of CNC machine tools
- About redis master-slave replication
- MFS distributed file system
- [webinar] mongodb and Google cloud accelerate enterprise digital innovation
- Shardingsphere sharding proxy actual combat scenario
猜你喜欢
![Contest3182 - the 39th individual training match for 2021 freshmen_ C: [string] ISBN number](/img/98/11ca12889a1b653ce8158920bf5136.jpg)
Contest3182 - the 39th individual training match for 2021 freshmen_ C: [string] ISBN number

Computing trends in three times of machine learning

China's Industrial Software Market Research Report is released, and SCADA and MES of force control enrich the ecology of domestic industrial software

Wanzhou gold industry: what are the differences between gold t+d investment and other investments?

MySQL中的行转列和列转行

Two methods of MySQL database login and logout

喜讯丨英方软件2022获得10项发明专利!

Application of scaleflux CSD 2000 in Ctrip
![[Tang Laoshi] C -- encapsulation: member method](/img/47/9a4ffd787624f6208b6aee66c38b48.jpg)
[Tang Laoshi] C -- encapsulation: member method

【网络研讨会】MongoDB 携手 Google Cloud 加速企业数字化创新
随机推荐
The power of code refactoring: how to measure the success of refactoring
Teach you how to realize pynq-z2 bar code recognition
How to rewrite tdengine code from 0 to 1 with vscode in "technical class"
Common optimization techniques for Web Performance
Wanzhou gold industry: what are the differences between gold t+d investment and other investments?
New products, new personnel and new services, Infiniti will continue to plough into China's future!
Seata server database connection user and service database undo_ What permissions do log users need?
Allocate aligned heap space
Analysis of shardingsphere core source code
On array-_-
[Tang Laoshi] C -- encapsulation: member method
[qt learning notes]qt uses MFC compilation to generate DLL libraries. The problem that DLL cannot be loaded occurs on computers without programming environment
Study on heritability and field experiment design
Daily leetcode force deduction (31~35)
Open source summer 2022 | opengauss project selected and announced
【协会通知】关于举办人工智能与物联网领域暑假专题师资培训的通知
ansible环境安装及数据恢复
JS event binding and common events
Recommend several open source IOT platforms
MongoDB和MySQL的区别