File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed
js/kotlinx-coroutines-core-js/src/main/kotlin/kotlinx/coroutines/experimental Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -36,14 +36,25 @@ public actual object Unconfined : CoroutineDispatcher() {
36
36
override fun toString (): String = " Unconfined"
37
37
}
38
38
39
+ private external val navigator: dynamic
40
+ private const val UNDEFINED = " undefined"
41
+
39
42
/* *
40
43
* This is the default [CoroutineDispatcher] that is used by all standard builders like
41
44
* [launch], [async], etc if no dispatcher nor any other [ContinuationInterceptor] is specified in their context.
42
45
*/
43
46
@Suppress(" PropertyName" , " UnsafeCastFromDynamic" )
44
47
public actual val DefaultDispatcher : CoroutineDispatcher = when {
48
+ // Check if we are running under ReactNative. We have to use NodeDispatcher under it.
49
+ // The problem is that ReactNative has a `window` object with `addEventListener`, but it does not really work.
50
+ // For details see https://github.com/Kotlin/kotlinx.coroutines/issues/236
51
+ // The check for ReactNative is based on https://github.com/facebook/react-native/commit/3c65e62183ce05893be0822da217cb803b121c61
52
+ jsTypeOf(navigator) != UNDEFINED && navigator != null && navigator.product == " ReactNative" ->
53
+ NodeDispatcher ()
45
54
// Check if we are in the browser and must use window.postMessage to avoid setTimeout throttling
46
- jsTypeOf(window) != " undefined" && jsTypeOf(window.asDynamic().addEventListener) != " undefined" -> window.asCoroutineDispatcher()
55
+ jsTypeOf(window) != UNDEFINED && window.asDynamic() != null && jsTypeOf(window.asDynamic().addEventListener) != UNDEFINED ->
56
+ window.asCoroutineDispatcher()
57
+ // Fallback to NodeDispatcher when browser environment is not detected
47
58
else -> NodeDispatcher ()
48
59
}
49
60
You can’t perform that action at this time.
0 commit comments