Chapter 4. It’s about time you used RxJS

 

This chapter covers

  • Understanding time in RxJS
  • Using time as a new dimension of your programs
  • Building observable streams with time
  • Learning about RxJS operators like debounce and throttle
  • Analyzing event data with buffering

Time is a tricky business. We spoke earlier about the challenges that exist when the code you write isn’t synchronous; it may have unpredictable wait times from one instruction to the next. We defined observables as infinite sequences of events, and now we add the last part of the puzzle to this definition—over time. The ancient Greek Heraclitus implied that time is always in motion, and so are observables.

Observables

are infinite sequences of events over time.

You can accurately measure the time a synchronous program takes by adding the execution time of its constituent functions, but this doesn’t hold for asynchronous programs because instructions aren’t linearly executed, as shown in figure 4.1.

Figure 4.1. In synchronous code (top), operations are predictable and typically depend on the input size and speed of the environment. Asynchronous programs (bottom) depend on many other factors, including the speed of the network.

4.1. Why worry about time?

4.2. Understanding asynchronous timing with JavaScript

4.3. Back to the future with RxJS

4.4. Handling user input

4.5. Buffering in RxJS

4.6. Summary

sitemap