Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Dispatchers.Main not being fully initialized on Android and Swing #3101

Merged
merged 4 commits into from Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt
Expand Up @@ -52,7 +52,7 @@ public sealed class HandlerDispatcher : MainCoroutineDispatcher(), Delay {
internal class AndroidDispatcherFactory : MainDispatcherFactory {

override fun createDispatcher(allFactories: List<MainDispatcherFactory>) =
HandlerContext(Looper.getMainLooper().asHandler(async = true))
HandlerContext(Looper.getMainLooper()!!.asHandler(async = true))
qwwdfsad marked this conversation as resolved.
Show resolved Hide resolved

override fun hintOnError(): String = "For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used"

Expand Down
10 changes: 10 additions & 0 deletions ui/kotlinx-coroutines-swing/src/SwingDispatcher.kt
Expand Up @@ -74,6 +74,16 @@ private object ImmediateSwingDispatcher : SwingDispatcher() {
* Dispatches execution onto Swing event dispatching thread and provides native [delay] support.
*/
internal object Swing : SwingDispatcher() {

/* A workaround so that the dispatcher's initialization crashes with an exception if running in a headless
environment. This is needed so that this broken dispatcher is not used as the source of delays. */
init {
Timer(1) { }.apply {
isRepeats = false
start()
}
}

override val immediate: MainCoroutineDispatcher
get() = ImmediateSwingDispatcher

Expand Down