Slide 61
Slide 61 text
Example 3 @Android
val UI = HandlerContext(Handler(Looper.getMainLooper()), "UI")
fun Handler.asCoroutineDispatcher() = HandlerContext(this)
public class HandlerContext(private val handler: Handler, private val name: String? = null) : CoroutineDispatcher(), Delay {
override fun dispatch(context: CoroutineContext, block: Runnable) {
handler.post(block)
}
override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation) {
handler.postDelayed({
with(continuation) { resumeUndispatched(Unit) }
}, unit.toMillis(time))
}
override fun invokeOnTimeout(time: Long, unit: TimeUnit, block: Runnable): DisposableHandle {
handler.postDelayed(block, unit.toMillis(time))
return object : DisposableHandle {
override fun dispose() {
handler.removeCallbacks(block)
}
}
}
override fun toString() = name ?: handler.toString()
override fun equals(other: Any?): Boolean = other is HandlerContext && other.handler === handler
override fun hashCode(): Int = System.identityHashCode(handler)
}