File tree 2 files changed +9
-6
lines changed
ui/kotlinx-coroutines-android
2 files changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -190,11 +190,14 @@ public suspend fun awaitFrame(): Long {
190
190
postFrameCallback(choreographer, cont)
191
191
}
192
192
}
193
- // post into looper thread to figure it out
194
193
return suspendCancellableCoroutine { cont ->
195
- Dispatchers . Main .dispatch( EmptyCoroutineContext , Runnable {
194
+ if ( Looper .myLooper() == = Looper .getMainLooper()) { // Check if we are already in the main looper thread
196
195
updateChoreographerAndPostFrameCallback(cont)
197
- })
196
+ } else { // post into looper thread to figure it out
197
+ Dispatchers .Main .dispatch(EmptyCoroutineContext , Runnable {
198
+ updateChoreographerAndPostFrameCallback(cont)
199
+ })
200
+ }
198
201
}
199
202
}
200
203
Original file line number Diff line number Diff line change @@ -109,14 +109,14 @@ class HandlerDispatcherTest : TestBase() {
109
109
launch(Dispatchers .Main , start = CoroutineStart .UNDISPATCHED ) {
110
110
expect(1 )
111
111
awaitFrame()
112
- expect(5 )
112
+ expect(3 )
113
113
}
114
114
expect(2 )
115
115
// Run choreographer detection
116
116
mainLooper.runOneTask()
117
- expect(3 )
118
- mainLooper.scheduler.advanceBy(50 , TimeUnit .MILLISECONDS )
119
117
expect(4 )
118
+ mainLooper.scheduler.advanceBy(50 , TimeUnit .MILLISECONDS )
119
+ expect(5 )
120
120
mainLooper.scheduler.advanceBy(51 , TimeUnit .MILLISECONDS )
121
121
finish(6 )
122
122
}
You can’t perform that action at this time.
0 commit comments