You are correct; switchMap
will unsubscribe from the Observable
returned by its project
argument as soon as it has invoked the project
function again to produce a new Observable
.
RxJs is incredibly powerful and dense, but its high level of abstraction can sometimes make code hard to understand. Let me debunk the marble diagrams and docs given by @Andy Hole a little and bring them up to date. You may find the marble syntax reference highly valuable to better understand rxjs operators from their tests (at least I found this missing/not highlighted enough in the official docs).
mergeMap
The first line in the diagram is the source Observable which emits (1,3,5)
at different times. The second line in the diagram is the prototypical Observable returned by the project
function i => ...
passed to the .mergeMap()
operator.
When the source Observable emits the item 1
, mergeMap()
invokes the project
function with i=1
. The returned Observable will emit 10
three times, every 10 frames (see marble syntax reference). The same happens when the source Observable emits item 3
and the project
function creates an Observable that emits 30
three times. Note that the result of mergeMap()
contains all three elements generated by each Observable returned from project
.
switchMap
This is different with switchMap()
, which will unsubscribe from the Observable returned by project
as soon as it has invoked it again on a new element. The marble diagram indicates this with the missing third 30
item in the output Observable.
In the example you have given, this leads to the cancellation of the pending search request. This is a very nice but hard-to-get-right property, which you get for free by combining switchMap()
with cancellable Observables returned by Angular's Http
service. This can save you a lot of headaches without worrying about properly handling all the race conditions that typically occur with async cancellation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…