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 參數限制一次活動内部訂閱的數量。