Chapter 6. Controlling the observer-observable relationship

 

This chapter covers

  • Creating observers without a fuss
  • Controlling the length of the observer/observable relationship
  • Adding and controlling side effects in the pipeline

Imagine you’re a singer with the most beautiful voice, the greatest lyrics, and the best performance moves. It doesn’t pay to be such a singer if you have no listeners. The same goes for observables; they sit there, doing nothing and wasting resources, if there’s no observer to subscribe to and receive their notifications. It’s also interesting to think about when the relationship between the singer and the listener begins and ends, especially if the music is coming from an album, and the listener can skip tracks or stop playing.

This chapter covers the methods you can use to create observers, and the importance of each action that observers need to implement. The subscription of the observer to the observable is also something you’ll need to maintain, but you can control when it begins and how long it lasts by the number of notifications or a timespan, a combination of the two, or with advanced logic that you define. All of these options are provided by a set of operators that you’ll learn about throughout this chapter. At the end of the chapter, you’ll combine them to create a fully reactive drawing application.

6.1. Creating observers

6.2. Controlling the observable-observer relationship lifetime

6.3. Putting it all together

6.4. Summary