javascript
RxJS mergeMap 学习笔记
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 參數限制一次活動內部訂閱的數量。
總結
以上是生活随笔為你收集整理的RxJS mergeMap 学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宏碁 X27U 显示器上架:27 英寸
- 下一篇: RxJs SwitchMap 学习笔记