Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion kotlinx-coroutines-core/js/src/CoroutineContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ internal actual fun createDefaultDispatcher(): CoroutineDispatcher = when {
// Check if we are in the browser and must use window.postMessage to avoid setTimeout throttling
jsTypeOf(window) != UNDEFINED && window.asDynamic() != null && jsTypeOf(window.asDynamic().addEventListener) != UNDEFINED ->
window.asCoroutineDispatcher()
// On react-native use setImmediate
jsTypeOf(navigator) != UNDEFINED && navigator != null && navigator.product == "ReactNative" ->
ReactNativeDispatcher
// If process is undefined (e.g. in NativeScript, #1404), use SetTimeout-based dispatcher
jsTypeOf(process) == UNDEFINED || jsTypeOf(process.nextTick) == UNDEFINED -> SetTimeoutDispatcher
// Fallback to NodeDispatcher when browser environment is not detected
// Fallback to NodeDispatcher when browser or react native environment is not detected
else -> NodeDispatcher
}

Expand Down
8 changes: 8 additions & 0 deletions kotlinx-coroutines-core/js/src/JSDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ internal object NodeDispatcher : SetTimeoutBasedDispatcher() {
}
}

internal object ReactNativeDispatcher : SetTimeoutBasedDispatcher() {
override fun scheduleQueueProcessing() {
setImmediate(messageQueue.processQueue)
}
}

internal actual class WindowMessageQueue actual constructor(private val window: W3CWindow) : MessageQueue() {
private val messageName = "dispatchCoroutine"

Expand Down Expand Up @@ -66,5 +72,7 @@ private external fun setTimeout(handler: dynamic, timeout: Int = definedExternal

private external fun clearTimeout(handle: Int = definedExternally)

private external fun setImmediate(f: () -> Unit)

private fun setTimeout(window: Window, handler: () -> Unit, timeout: Int): Int =
window.setTimeout(handler, timeout)