Thinking in streams

So far, we have gone through a bunch of scenarios that have shown us which operators are at our disposal and how they can be chained. We have also seen how operators such as flatMap() and switchMap() can really change things as we move from one type of observable to another. So, which approach should you take when working with Observables? Obviously, we need to express an algorithm using operators, but where do we start? The first thing we need to do is to think of the start and the end. Which types of events do we want to capture and what should the end result look like? That already gives us a hint as to the number of transformations we need to carry out to get there. If we want to transform the data only, then we can probably make do with a map() operator and a filter() operator. If we want to transform from one Observable to the next, then we need a flatMap() or a switchMap(). Do we have a specific behavior, such as waiting for the user to stop typing? If so, then we need to look at debounceTime() or similar. It's really the same as all problems: break it down, see which parts you have, divide, and conquer. Let's try to break this down into a list of steps though:

  • What are the inputs? UI events or something else?
  • What are the outputs? The end result?
  • Given the second bullet, which transformations do I need to get there?
  • Do I deal with more than one stream?
  • Do I need to handle errors, and if so, how? 

This has hopefully introduced you to how to think about streams. Remember, start small and work your way toward your goal.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.59.245