@@ -7,24 +7,19 @@ package kotlinx.coroutines
7
7
import kotlin.coroutines.*
8
8
9
9
public actual object Dispatchers {
10
-
11
10
public actual val Default : CoroutineDispatcher = createDefaultDispatcher()
12
-
13
- public actual val Main : MainCoroutineDispatcher = JsMainDispatcher (Default )
14
-
11
+ public actual val Main : MainCoroutineDispatcher = JsMainDispatcher (Default , false )
15
12
public actual val Unconfined : CoroutineDispatcher = kotlinx.coroutines.Unconfined
16
13
}
17
14
18
- private class JsMainDispatcher (val delegate : CoroutineDispatcher ) : MainCoroutineDispatcher() {
19
-
20
- override val immediate: MainCoroutineDispatcher
21
- get() = throw UnsupportedOperationException (" Immediate dispatching is not supported on JS" )
22
-
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 )
21
+ override fun isDispatchNeeded (context : CoroutineContext ): Boolean = ! invokeImmediately
23
22
override fun dispatch (context : CoroutineContext , block : Runnable ) = delegate.dispatch(context, block)
24
-
25
- override fun isDispatchNeeded (context : CoroutineContext ): Boolean = delegate.isDispatchNeeded(context)
26
-
27
23
override fun dispatchYield (context : CoroutineContext , block : Runnable ) = delegate.dispatchYield(context, block)
28
-
29
24
override fun toString (): String = toStringInternalImpl() ? : delegate.toString()
30
25
}
0 commit comments