1 /*
2  * Copyright 2016-2020 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 kotlin.coroutines.*
8 
9 public actual object Dispatchers {
10     public actual val Default: CoroutineDispatcher = createDefaultDispatcher()
11     public actual val Main: MainCoroutineDispatcher = JsMainDispatcher(Default, false)
12     public actual val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined
13 }
14 
15 private class JsMainDispatcher(
16     val delegate: CoroutineDispatcher,
17     private val invokeImmediately: Boolean
18 ) : MainCoroutineDispatcher() {
19     override val immediate: MainCoroutineDispatcher =
20         if (invokeImmediately) this else JsMainDispatcher(delegate, true)
isDispatchNeedednull21     override fun isDispatchNeeded(context: CoroutineContext): Boolean = !invokeImmediately
22     override fun dispatch(context: CoroutineContext, block: Runnable) = delegate.dispatch(context, block)
23     override fun dispatchYield(context: CoroutineContext, block: Runnable) = delegate.dispatchYield(context, block)
24     override fun toString(): String = toStringInternalImpl() ?: delegate.toString()
25 }
26