Skip to content

Commit 38dd273

Browse files
committed
Make awaitFrame in android module immediate-dispatcher friendly
1 parent f7f65dc commit 38dd273

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -191,24 +191,23 @@ public suspend fun awaitFrame(): Long {
191191
}
192192
}
193193
return suspendCancellableCoroutine { cont ->
194-
if (Looper.myLooper() === Looper.getMainLooper()) { // Check if we are already in the main looper thread
195-
updateChoreographerAndPostFrameCallback(cont)
196-
} else { // post into looper thread to figure it out
194+
if (Dispatchers.Main.immediate.isDispatchNeeded(cont.context)) {
197195
Dispatchers.Main.dispatch(EmptyCoroutineContext, Runnable {
198196
updateChoreographerAndPostFrameCallback(cont)
199197
})
198+
} else {
199+
updateChoreographerAndPostFrameCallback(cont)
200200
}
201201
}
202202
}
203203

204204
private fun updateChoreographerAndPostFrameCallback(cont: CancellableContinuation<Long>) {
205-
val choreographer = choreographer ?:
206-
Choreographer.getInstance()!!.also { choreographer = it }
205+
val choreographer = choreographer ?: Choreographer.getInstance()!!.also { choreographer = it }
207206
postFrameCallback(choreographer, cont)
208207
}
209208

210209
private fun postFrameCallback(choreographer: Choreographer, cont: CancellableContinuation<Long>) {
211210
choreographer.postFrameCallback { nanos ->
212-
with(cont) { Dispatchers.Main.resumeUndispatched(nanos) }
211+
with(cont) { Dispatchers.Main.immediate.resumeUndispatched(nanos) }
213212
}
214213
}

0 commit comments

Comments
 (0)