Alexandru Nedelcu
07/18/2024, 10:02 AMjava.util.concurrent.Executor
, which isn't an issue, as I just defined my own for JS. The problem is that I need back-and-forth conversions between my Executor
and CoroutineDispatcher
.
So on top of the JVM, this isn't an issue:
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.asExecutor
dispatcher.asExecutor()
executor.asCoroutineDispatcher()
But on top of JS, I need to know if these functions are doing the right thing, especially since the JVM implementation seems to be more complicated.
So for converting a CoroutineDispatcher
into an Executor
, I'm doing this:
fun dispatcherAsExecutor(dispatcher: CoroutineDispatcher): Executor =
object : Executor {
override fun execute(command: Runnable) {
dispatcher.dispatch(
EmptyCoroutineContext,
command
)
}
}
Or for converting an Executor
into a CoroutineDispatcher
:
fun executorAsDispatcher(executor: Executor): CoroutineDispatcher =
object : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
executor.execute(block)
}
}
Where I'm having issues, especially in the latter case, is that I don't know the purpose of the CoroutineContext
here, especially as it gets ignored — i.e., I hope there isn't some mechanism for it to be passed around that I'm ignoring.
IS THIS OK?
Thanks.