Skip to content

Commit b85577c

Browse files
qwwdfsadrecheej
authored andcommitted
Cherry-picks from native-mt branch to reduce maintenance burden (Kotlin#2204)
* Immediate dispatcher on JS, test added * Style fixes
1 parent d67e597 commit b85577c

File tree

6 files changed

+42
-26
lines changed

6 files changed

+42
-26
lines changed

buildSrc/src/main/kotlin/Idea.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
object Idea {
2+
@JvmStatic // for Gradle
23
val active: Boolean
34
get() = System.getProperty("idea.active") == "true"
45
}

kotlinx-coroutines-core/js/src/Dispatchers.kt

+8-13
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,19 @@ package kotlinx.coroutines
77
import kotlin.coroutines.*
88

99
public actual object Dispatchers {
10-
1110
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)
1512
public actual val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined
1613
}
1714

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
2322
override fun dispatch(context: CoroutineContext, block: Runnable) = delegate.dispatch(context, block)
24-
25-
override fun isDispatchNeeded(context: CoroutineContext): Boolean = delegate.isDispatchNeeded(context)
26-
2723
override fun dispatchYield(context: CoroutineContext, block: Runnable) = delegate.dispatchYield(context, block)
28-
2924
override fun toString(): String = toStringInternalImpl() ?: delegate.toString()
3025
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.test.*
8+
9+
class ImmediateDispatcherTest : TestBase() {
10+
11+
@Test
12+
fun testImmediate() = runTest {
13+
expect(1)
14+
val job = launch { expect(3) }
15+
withContext(Dispatchers.Main.immediate) {
16+
expect(2)
17+
}
18+
job.join()
19+
finish(4)
20+
}
21+
22+
@Test
23+
fun testMain() = runTest {
24+
expect(1)
25+
val job = launch { expect(2) }
26+
withContext(Dispatchers.Main) {
27+
expect(3)
28+
}
29+
job.join()
30+
finish(4)
31+
}
32+
}

kotlinx-coroutines-core/jvm/src/Executors.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import kotlin.coroutines.*
1414
* Instances of [ExecutorCoroutineDispatcher] should be closed by the owner of the dispatcher.
1515
*
1616
* This class is generally used as a bridge between coroutine-based API and
17-
* asynchronous API which requires instance of the [Executor].
17+
* asynchronous API that requires an instance of the [Executor].
1818
*/
1919
public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closeable {
2020
/** @suppress */

kotlinx-coroutines-core/native/src/Dispatchers.kt

-8
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@ package kotlinx.coroutines
77
import kotlin.coroutines.*
88

99
public actual object Dispatchers {
10-
1110
public actual val Default: CoroutineDispatcher = createDefaultDispatcher()
12-
1311
public actual val Main: MainCoroutineDispatcher = NativeMainDispatcher(Default)
14-
1512
public actual val Unconfined: CoroutineDispatcher get() = kotlinx.coroutines.Unconfined // Avoid freezing
1613
}
1714

1815
private class NativeMainDispatcher(val delegate: CoroutineDispatcher) : MainCoroutineDispatcher() {
19-
2016
override val immediate: MainCoroutineDispatcher
2117
get() = throw UnsupportedOperationException("Immediate dispatching is not supported on Native")
22-
2318
override fun dispatch(context: CoroutineContext, block: Runnable) = delegate.dispatch(context, block)
24-
2519
override fun isDispatchNeeded(context: CoroutineContext): Boolean = delegate.isDispatchNeeded(context)
26-
2720
override fun dispatchYield(context: CoroutineContext, block: Runnable) = delegate.dispatchYield(context, block)
28-
2921
override fun toString(): String = toStringInternalImpl() ?: delegate.toString()
3022
}

ui/kotlinx-coroutines-android/build.gradle.kts

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import org.jetbrains.dokka.DokkaConfiguration.ExternalDocumentationLink
66
import org.jetbrains.dokka.gradle.DokkaTask
77
import java.net.URL
88

9-
repositories {
10-
google()
11-
}
12-
139
configurations {
1410
create("r8")
1511
}

0 commit comments

Comments
 (0)