I want to implement the following functions in the most re-active way. I need these for implementing the bijections for automatic conversion between the said types.
def convertScalaRXObservableToTwitterFuture[A](a: Observable[A]): TwitterFuture[A] = ???
def convertScalaRXObservableToTwitterFutureList[A](a: Observable[A]): TwitterFuture[List[A]] = ???
I came across this article on a related subject but I can't get it working.
Unfortunately the claim in that article is not correct and there can't be a true bijection between
Observable
and anything likeFuture
. The thing is thatObservable
is more powerful abstraction that can represent things that can't be represented byFuture
. For example,Observable
might actually represent an infinite sequence. For example seeObservable.interval
. Obviously there is no way to represent something like this with aFuture
. TheObservable.toList
call used in that article explicitly mentions that:and later it says:
Even if you limit yourself to only finite
Observable
s, stillFuture
can't fully express semantics ofObservable
. ConsiderObservable.intervalRange
that generates a limited range one by one over some time period. WithObservable
the first event comes afterinitialDelay
and then you get event eachperiod
. WithFuture
you can get only one event and it must be only when the sequence is fully generated soObservable
is completed. It means that by transformingObservable[A]
intoFuture[List[A]]
you immediately break the main benefit ofObservable
- reactivity: you can't process events one by one, you have to process them all in a single bunch.To sum up the claim at the first paragraph of the article:
is false because conversion
Observable[A]
->Future[List[A]]
exactly looses the "event-driven nature" ofObservable
and there is no way to work this around.P.S. Actually the fact that
Future
is less powerful thanObservable
should not be a big surprise. If it was not, why anybody would createObservable
in the first place?