Exerosis
05/05/2023, 5:24 PMrunBlocking(MainThreadDispatcher) {
someLongRunningProcessThatSuspends(...)
}
I need to have an event loop inside of the dispatcher separate to the java one. So I have logic like:
override fun dispatch(context: CoroutineContext, block: Runnable) {
if (eventLoopIsBlocking) {
eventLoopQueue.push(block)
eventLoopCondition.signal()
} else if (isOnMainThread()) block.run()
else mainThreadScheduler.runOnNextCycle(block)
}
That's fine but what about:
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
//here we probably need to know if the event loop would be waiting for this "delay"
mainThreadScheduler.runAfterCycles(millisToCycles(timeMillis)) { continuation.resume(Unit) }
}
This logic works if you are on another thread and do like:
//some other thread/dispatcher
withContext(MainThreadDispatcher) {
delay(3.seconds) //this will eventually be called, although it may be delayed further by the event loop taking a long time.
somethingThatNeedsToBeRunFromMainThread()
}
but won't work if you have:
//we are on main thread here.
runBlocking(MainThreadDispatcher) {
delay(3.seconds) //this will never be resumed.
}
So I guess I'm not sure how to make this work cleanly.