Any coroutine-gurus here than can help? I've wrap...
# kotlin-native
a
Any coroutine-gurus here than can help? I've wrapped a C-library method, that feeds values to a callback, in a kotlin method to capture the results (callback will be called multiple times) in a lambda. Basically called like this:
Copy code
library.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:
Copy code
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?
Thanks! Will do