Composing Observable objects
Having seen different ways of creating various types of the Observable
objects, subscribing to their events, and using the Subscription
objects, we turn our attention to composing the Observable
objects into larger programs. From what we have seen so far, the advantages of using the Observable
objects over a callback-based API are hardly worth the trouble.
The true power of Rx becomes apparent when we start composing the Observable
objects using various combinators. We can think of an Observable
object in a similar way as we think of Scala sequence collections. In a Scala sequence, represented by the Seq[T]
trait, elements of type T
are ordered in the memory according to their indices. In an Observable[T]
trait, events of type T
are ordered in time.
Let's use the Observable.interval
factory method in order to create an Observable
object, which asynchronously emits a number every 0.5 seconds, and then output the first five odd numbers. To do this, we first call...