appleMain.org.brightify.hyperdrive.Dispatcher.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime Show documentation
Show all versions of runtime Show documentation
Hyperdrive implementation that's needed for observations and such
package org.brightify.hyperdrive
import kotlinx.coroutines.*
import platform.darwin.*
import kotlin.coroutines.CoroutineContext
@OptIn(InternalCoroutinesApi::class)
internal actual fun dispatcher(): CoroutineDispatcher = UI
@InternalCoroutinesApi
private object UI: CoroutineDispatcher(), Delay {
override fun dispatch(context: CoroutineContext, block: Runnable) {
val queue = dispatch_get_main_queue()
dispatch_async(queue) {
block.run()
}
}
@OptIn(ExperimentalCoroutinesApi::class)
override fun scheduleResumeAfterDelay(
timeMillis: Long,
continuation: CancellableContinuation
) {
val queue = dispatch_get_main_queue()
val time = dispatch_time(DISPATCH_TIME_NOW, (timeMillis * NSEC_PER_MSEC.toLong()))
dispatch_after(time, queue) {
with(continuation) {
resumeUndispatched(Unit)
}
}
}
}