Turn a synchronous multi-shot call-back function into a Sequence
The following function shall represent a replacement for some library funtion, so there is no control over it:
fun loop(callback: (Int) -> Unit) {
var i = 0
while (true) {
Thread.sleep(100)
callback(i++)
}
}
I want to wrap this and work with a Sequence instead. I know callbackFlow from kotlinx.coroutines which can be used to turn this into a Flow, roughly like this:
fun main() {
val flow = callbackFlow {
loop {
trySendBlocking(it)...