|
| 1 | +/* |
| 2 | + * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.coroutines |
| 6 | + |
| 7 | +import kotlinx.coroutines.internal.* |
| 8 | +import org.w3c.dom.* |
| 9 | +import kotlin.coroutines.* |
| 10 | + |
| 11 | +internal external interface JsProcess : JsAny { |
| 12 | + fun nextTick(handler: () -> Unit) |
| 13 | +} |
| 14 | + |
| 15 | +internal fun tryGetProcess(): JsProcess? = |
| 16 | + js("(typeof(process) !== 'undefined' && typeof(process.nextTick) === 'function') ? process : null") |
| 17 | + |
| 18 | +internal fun tryGetWindow(): Window? = |
| 19 | + js("(typeof(window) !== 'undefined' && window != null && typeof(window.addEventListener) === 'function') ? window : null") |
| 20 | + |
| 21 | +internal fun createDefaultDispatcher(): CoroutineDispatcher = |
| 22 | + tryGetProcess()?.let(::NodeDispatcher) |
| 23 | + ?: tryGetWindow()?.let(::WindowDispatcher) |
| 24 | + ?: SetTimeoutDispatcher |
| 25 | + |
| 26 | +@PublishedApi // Used from kotlinx-coroutines-test via suppress, not part of ABI |
| 27 | +internal actual val DefaultDelay: Delay |
| 28 | + get() = Dispatchers.Default as Delay |
| 29 | + |
| 30 | +public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext { |
| 31 | + val combined = coroutineContext + context |
| 32 | + return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null) |
| 33 | + combined + Dispatchers.Default else combined |
| 34 | +} |
| 35 | + |
| 36 | +public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext { |
| 37 | + return this + addedContext |
| 38 | +} |
| 39 | + |
| 40 | +// No debugging facilities on JS |
| 41 | +internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block() |
| 42 | +internal actual inline fun <T> withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T = block() |
| 43 | +internal actual fun Continuation<*>.toDebugString(): String = toString() |
| 44 | +internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on JS |
| 45 | + |
| 46 | +internal actual class UndispatchedCoroutine<in T> actual constructor( |
| 47 | + context: CoroutineContext, |
| 48 | + uCont: Continuation<T> |
| 49 | +) : ScopeCoroutine<T>(context, uCont) { |
| 50 | + override fun afterResume(state: Any?) = uCont.resumeWith(recoverResult(state, uCont)) |
| 51 | +} |
0 commit comments