Skip to content

Commit

Permalink
fix default dispatcher for react native
Browse files Browse the repository at this point in the history
  • Loading branch information
Legion2 committed Jul 29, 2021
1 parent ea440c5 commit 04fcba8
Showing 1 changed file with 1 addition and 7 deletions.
8 changes: 1 addition & 7 deletions kotlinx-coroutines-core/js/src/CoroutineContext.kt
Expand Up @@ -13,12 +13,6 @@ private const val UNDEFINED = "undefined"
internal external val process: dynamic

internal actual fun createDefaultDispatcher(): CoroutineDispatcher = when {
// Check if we are running under ReactNative. We have to use NodeDispatcher under it.
// The problem is that ReactNative has a `window` object with `addEventListener`, but it does not really work.
// For details see https://github.com/Kotlin/kotlinx.coroutines/issues/236
// The check for ReactNative is based on https://github.com/facebook/react-native/commit/3c65e62183ce05893be0822da217cb803b121c61
jsTypeOf(navigator) != UNDEFINED && navigator != null && navigator.product == "ReactNative" ->
NodeDispatcher
// Check if we are running under jsdom. WindowDispatcher doesn't work under jsdom because it accesses MessageEvent#source.
// It is not implemented in jsdom, see https://github.com/jsdom/jsdom/blob/master/Changelog.md
// "It's missing a few semantics, especially around origins, as well as MessageEvent source."
Expand All @@ -27,7 +21,7 @@ internal actual fun createDefaultDispatcher(): CoroutineDispatcher = when {
jsTypeOf(window) != UNDEFINED && window.asDynamic() != null && jsTypeOf(window.asDynamic().addEventListener) != UNDEFINED ->
window.asCoroutineDispatcher()
// If process is undefined (e.g. in NativeScript, #1404), use SetTimeout-based dispatcher
jsTypeOf(process) == UNDEFINED -> SetTimeoutDispatcher
jsTypeOf(process) == UNDEFINED || jsTypeOf(process.nextTick) == UNDEFINED -> SetTimeoutDispatcher
// Fallback to NodeDispatcher when browser environment is not detected
else -> NodeDispatcher
}
Expand Down

0 comments on commit 04fcba8

Please sign in to comment.