当前位置:网站首页>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 :

边栏推荐
- Using WebDAV instead of 445 port file share
- Allocate aligned heap space
- Seata server database connection user and service database undo_ What permissions do log users need?
- On array-_-
- 产学合作协同育人,麒麟软件携手南开大学合力完成《软件测试与维护》实践课程
- If the storage engine of time series database wants to be the best, it has to do its own research
- Bit. Store: long bear market, stable stacking products may become the main theme
- Alibaba's mission, vision and core values
- Summary of domestic database certification test guide (updated on June 16, 2022)
- Contest3182 - the 39th individual training match for 2021 freshmen_ E: ringring
猜你喜欢

How to arrange digital collections on online platforms such as reading and Chinese online? Will "read/write-to-earn" products be launched in the future?

数据分析师太火?月入3W?用数据告诉你这个行业的真实情况

im即时通讯开发之双进程守护保活实践

MySQL中的行转列和列转行

【网络研讨会】MongoDB 携手 Google Cloud 加速企业数字化创新

Row to column and column to row in MySQL

如何实现IM即时通讯“消息”列表卡顿优化

TP5 restrict access frequency

TDengine在数控机床监控中的应用

Analysis of shardingsphere core source code
随机推荐
MySQL中的行转列和列转行
Application practice of day13 for loop distinguish the application of traversing break continue
Win10 LTSC 2021 wsappx CPU usage high
[UVM basics] UVM tree organizational structure
国产数据库认证考试指南汇总(2022年6月16日更新)
Simple anti shake for wechat applet
Push NFT out of the regulatory dilemma, and BSN launched NFT supporting infrastructure network
Wanzhou gold industry: a common technical term in gold t+d transaction?
[UVM foundation] can only be used in build_ Research on executing instantiation action in phase
利用OpenCV执行相机校准
Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
How to create a login interface
Open source summer 2022 | opengauss project selected and announced
实现时序数据库(Time Series Database)在特定场景下“远超”通用数据库的难点
TDengine 连接器上线 Google Data Studio 应用商店
【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩
脉脉热帖:为啥大厂都热衷于造轮子?
[UVM foundation] UVM_ Is in agent_ Active variable definition
Hi,你有一份Code Review攻略待查收!
Hikvision tools manager Hikvision tools collection (including sadp, video capacity calculation and other tools) a practical tool for millions of security practitioners