Quick review of part 1, our purpose is to clarify differences between RxJs mergeMap, switchMap, concatMap, exhaustMap, and we made a scenario as bellow to do this:
- There are 5 tasks which I have to do on almost every morning
- The 5 tasks start in a specific order, which won't change at all.
- But how long each task takes to finish it, that will change.
- Our mappers wait for a task finish and emit message to info the finish.
Let's explain the rest 2 mappers: switchMap and exhaustMap.
switchMap
switchMap is like a kid with ADHD, always finds the new toy more amusing, and ditch the old ones. Every time switchMap received the new item from its source, it canceled those subscriptions which still waiting. Only those still waiting are cancelled, I repeat, something passed is already passed, not relevant.
The code
The result
Because each of our tasks takes some time to finish it, that is, when switchMap receives the Start signal of task 5, all the other tasks are still waiting for their works done, and task 5 is the last one, so switchMap ditches other tasks, then only finishes task 5.
exhaustMap
After exhaustMap receives first item, and before it finishes its job, it'll ignore any thing coming in this period of time. It's like "Don't bother me, I'm still working on your shit".
The code
The result
Here you can see it clearly that after exhaustMap start task 1, it stops hearing from outside world, delete its email account and throw away its mobile, until task 1 finishes. exhaustMap won't even bother to start other tasks, bad, bad exhaustMap.
No comments:
Post a Comment