So I'm pretty new to both Scala and RX. The guy who knew the most, and who actually wrote this code, just left, and I'm not sure what's going on. This construct is all over his code and I'm not really clear what its doing:
def foo(List[Long]) : Observable[Unit] =
Observable {
subscriber => {
do some stuff
subscriber.onNext()
subscriber.onCompleted()
}
I mostly get do some stuff
, and the calls to subscriber. What I don't get is, where does subscriber
come from? Does subscriber => {
instantiate the subscriber? What does Observable { subscriber => { ... } }
do/mean?
If you take a look at the Observable companion object documentation, you will see an apply
method that takes a function of type (Subscriber[T]) ⇒ Unit
. So, when you call Observable{withSomeLambda}
, then this is the same as calling Observable.apply{withSomeLambda}
And, if you go all the way to the source code you will see that this is really returning
toScalaObservable(rx.Observable.create(f))
where f
is the lambda that you passed in.
So, subscriber
is just the parameter of the lambda. It is passed in by the caller of that function.