package kotlinx.coroutines import kotlinx.coroutines.internal.* import org.w3c.dom.* import kotlin.coroutines.* internal external interface JsProcess : JsAny { fun nextTick(handler: () -> Unit) } internal fun tryGetProcess(): JsProcess? = js("(typeof(process) !== 'undefined' && typeof(process.nextTick) === 'function') ? process : null") internal fun tryGetWindow(): Window? = js("(typeof(window) !== 'undefined' && window != null && typeof(window.addEventListener) === 'function') ? window : null") internal actual fun createDefaultDispatcher(): CoroutineDispatcher = tryGetProcess()?.let(::NodeDispatcher) ?: tryGetWindow()?.let(::WindowDispatcher) ?: SetTimeoutDispatcher @PublishedApi // Used from kotlinx-coroutines-test via suppress, not part of ABI internal actual val DefaultDelay: Delay get() = Dispatchers.Default as Delay public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext { val combined = coroutineContext + context return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null) combined + Dispatchers.Default else combined } public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext { return this + addedContext } // No debugging facilities on Wasm internal actual inline fun withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block() internal actual inline fun withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T = block() internal actual fun Continuation<*>.toDebugString(): String = toString() internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on Wasm internal actual class UndispatchedCoroutine actual constructor( context: CoroutineContext, uCont: Continuation ) : ScopeCoroutine(context, uCont) { override fun afterResume(state: Any?) = uCont.resumeWith(recoverResult(state, uCont)) }