当前位置:网站首页>Usage of rxjs mergemap
Usage of rxjs mergemap
2022-06-27 18:51:00 【WangZiXi】
Be careful :
- flatMap yes mergeMap Another name for .
- If only one internal subscription can be activated at a time , Please use switchMap.
- If inside observables The order of launch and subscription is very important , Please use concatMap.
When you need to flatten the inside observable But when you want to manually control the number of internal subscriptions , yes mergeMap Excellent use occasion .
for example , When using switchMap when , Each internal subscription is completed when the feed is issued , That is, only one active internal subscription is allowed in any time period . by comparison ,mergeMap Allow multiple internal subscriptions to be activated at the same time . therefore ,mergeMap One of the most common use cases is a request that should not be cancelled , These requests are considered write rather than read .
A typical example is SAP Amount of different items in e-commerce cloud shopping cart , You can increase or decrease in parallel .
Please note that , If these writes must remain in order , be concatMap It's a better choice . For example, database write operation .
because mergeMap Maintain internal subscriptions for multiple activities at once , Therefore, memory leaks may occur due to long-standing internal subscriptions . A basic example is , If you use an internal timer or dom Event flows are mapped to observable objects . In these cases , If you still want to use mergeMap, A good idea is to use another operator to manage the completion of internal subscriptions , Consider, for example take or takeUntil. You can use it, of course concurrent Parameter limits the number of internal subscriptions to an activity .
Take an example :
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 It's a function , It can take any incoming input parameter , Wrap into one Observable, And this Observable Not immediately emit data , It's a delay 500 millisecond .

mergeMap Receive a function as an input parameter , The input parameter of this function is through pipe link mergeMap Of Observable The elements contained in , namely MouseEvent;project The data type returned is a new Observable, Contains screen clicks X and Y Coordinates and current timestamp .

The final output :

边栏推荐
- Two methods of MySQL database login and logout
- 技术分享 | kubernetes pod 简介
- Technology sharing | introduction to kubernetes pod
- 阅文、中文在线等网文平台如何布局数字藏品?未来是否会推出“Read/Write-to-Earn”产品?
- ansible环境安装及数据恢复
- Application of scaleflux CSD 2000 in Ctrip
- How can Seata performance be improved? For example, add a computing node to the database?
- MySQL数据库登录和退出的两种方式
- Oracle的NUMBER长度超过19之后,实体使用Long映射,导致出现问题是为什么?
- The first in China! EMQ joined Amazon cloud technology's "startup acceleration - global partner network program"
猜你喜欢

On array-_-

Lvgl8.x migrating to stm32f4
![[JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes](/img/cb/0be2b2cf3b2cfa903190b7795d3cd7.jpg)
[JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes

Mise à jour SQL mise à jour par lots

原创 | 2025实现“5个1”奋斗目标!解放动力全系自主非道路国四产品正式发布

Industry university cooperation cooperates to educate people, and Kirin software cooperates with Nankai University to complete the practical course of software testing and maintenance

MFS分布式文件系统

数据同步工具 DataX 已经正式支持读写 TDengine
![[notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things](/img/ef/5b86170e60a7e03c4db512445540b9.jpg)
[notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things

Rxjs mergeMap 的使用场合
随机推荐
Teach you to use elastic search: run the first hello world search command
Recommend several open source IOT platforms
MongoDB和MySQL的区别
如何制作登录界面
Good news - British software 2022 has obtained 10 invention patents!
TDengine在数控机床监控中的应用
Wanzhou gold industry: a common technical term in gold t+d transaction?
Teach you how to install Oracle 19C on Windows 10 (detailed picture and text with step on pit Guide)
脉脉热帖:为啥大厂都热衷于造轮子?
IDEA 官网插件地址
Optimal binary search tree
【ELT.ZIP】OpenHarmony啃论文俱乐部—见证文件压缩系统EROFS
时序数据库(Time Series Database)的存储引擎要想做到极致,还得自研
Wanzhou gold industry: what are the differences between gold t+d investment and other investments?
Set up your own website (10)
时间序列数据的特点
Contest3182 - the 39th individual training match for 2021 freshmen_ F: ss
Technology sharing | introduction to kubernetes pod
如何实现IM即时通讯“消息”列表卡顿优化
Teach you to use elastic search: run the first hello world search command