ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

RxJS mergeMap 学习笔记

2021-07-08 14:05:31  阅读:193  来源: 互联网

标签:mergeMap 订阅 内部 subscriptions 笔记 inner RxJS active


https://www.learnrxjs.io/learn-rxjs/operators/transformation/mergemap

This operator is best used when you wish to flatten an inner observable but want to manually control the number of inner subscriptions.

使用场合:当您希望展平内部 observable 但想要手动控制内部订阅的数量时

For instance, when using switchMap each inner subscription is completed when the source emits, allowing only one active inner subscription. In contrast, mergeMap allows for multiple inner subscriptions to be active at a time. Because of this, one of the most common use-case for mergeMap is requests that should not be canceled, think writes rather than reads. Note that if order must be maintained concatMap is a better option.

例如,当使用 switchMap 时,每个内部订阅在源发出时完成,只允许一个活动的内部订阅。 相比之下,mergeMap 允许同时激活多个内部订阅。 因此,mergeMap 最常见的用例之一是不应取消的请求,认为是写入而不是读取。 请注意,如果必须维护顺序,则 concatMap 是更好的选择。

注意内存泄漏问题。

Be aware that because mergeMap maintains multiple active inner subscriptions at once it’s possible to create a memory leak through long-lived inner subscriptions. A basic example would be if you were mapping to an observable with an inner timer, or a stream of dom events. In these cases, if you still wish to utilize mergeMap you may want to take advantage of another operator to manage the completion of the inner subscription, think take or takeUntil. You can also limit the number of active inner subscriptions at a time with the concurrent parameter

由于 mergeMap 一次维护多个活动的内部订阅,因此可能会通过长期存在的内部订阅造成内存泄漏。 一个基本示例是,如果您要映射到具有内部计时器的 observable 或 dom 事件流。 在这些情况下,如果您仍然希望使用 mergeMap,您可能希望利用另一个操作符来管理内部订阅的完成,考虑 take 或 takeUntil。 您还可以使用 concurrent 参数限制一次活动内部订阅的数量。

标签:mergeMap,订阅,内部,subscriptions,笔记,inner,RxJS,active
来源: https://blog.51cto.com/jerrywangsap/3012907

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有