andreasmattsson
09/01/2017, 8:26 AMlibrary.invoke(arg) { value -> doSomething(value) }
Now what I'd like to do is wrap this somehow so that instead of using a callback my wrapping method returns a Sequence (going from a push to a pull style API).
My first attempt was to use buildSequence:
buildSequence { library.invoke(arg) { value -> yield(value) } }
But this gives Suspension functions can be called only within coroutine body
.
The Wrapping callbacks section in the documentation here looked promising:
https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#wrapping-callbacks
But as far as I can tell this suspendCoroutine
trick only works if the callback is called only once by the library. Mine is called many many times, and I want to have a sequence where I can consume items one by one and not have to gather them in a list first.
Channels looks like a good approach but AFAIK they are only available in the kotlinx.coroutines
package that does not work in Kotlin Native.
Any suggestions? Are there any "out of the box" or idiomatic solutions for this that don't involve manually implementing a producer/consumer pattern using C/threading concurrency stuff?andreasmattsson
09/01/2017, 9:47 AM