While reading the Scala.React implementation on GitHub I've stumbled across the @suspendable annotation:
object Reactor {
def loop[A](op: FlowOps => Unit @suspendable): Reactor = new Reactor {
def body = while (!isDisposed) op(this)
}
}
In the paper Deprecating the Observer Pattern with Scala.React, the Reactor object is used in the following way:
Reactor.loop { self =>
// step 1
val path = new Path((self await mouseDown).position)
self.loopUntil(mouseUp) { // step 2
path.lineTo(m.position)
draw(path)
}
path.close() // step 3
draw(path)
}
Notably, the code in the Reactor body can wait for Events, like mouseDown. Therefore it looks like the code is executed asynchronously, even though there is no explicit use of threads. Because I couldn't find what the @suspandable annotation does, I feel like I'd need to understand it, before I can understand the rest of the implementation. Therefore:
- What does the
@suspendableannotation do? - Are there scenarios, where it is required?
- When would I use it?
My suspicion is, that it somehow abstracts over, and allows for asynchronous execution. If that is true:
- How does it work "under the hood" / how is it implemented?