Skip to content

Cherry-picks from native-mt branch to reduce maintenance burden #2204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Idea.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
object Idea {
@JvmStatic // for Gradle
val active: Boolean
get() = System.getProperty("idea.active") == "true"
}
21 changes: 8 additions & 13 deletions kotlinx-coroutines-core/js/src/Dispatchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ package kotlinx.coroutines
import kotlin.coroutines.*

public actual object Dispatchers {

public actual val Default: CoroutineDispatcher = createDefaultDispatcher()

public actual val Main: MainCoroutineDispatcher = JsMainDispatcher(Default)

public actual val Main: MainCoroutineDispatcher = JsMainDispatcher(Default, false)
public actual val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined
}

private class JsMainDispatcher(val delegate: CoroutineDispatcher) : MainCoroutineDispatcher() {

override val immediate: MainCoroutineDispatcher
get() = throw UnsupportedOperationException("Immediate dispatching is not supported on JS")

private class JsMainDispatcher(
val delegate: CoroutineDispatcher,
private val invokeImmediately: Boolean
) : MainCoroutineDispatcher() {
override val immediate: MainCoroutineDispatcher =
if (invokeImmediately) this else JsMainDispatcher(delegate, true)
override fun isDispatchNeeded(context: CoroutineContext): Boolean = !invokeImmediately
override fun dispatch(context: CoroutineContext, block: Runnable) = delegate.dispatch(context, block)

override fun isDispatchNeeded(context: CoroutineContext): Boolean = delegate.isDispatchNeeded(context)

override fun dispatchYield(context: CoroutineContext, block: Runnable) = delegate.dispatchYield(context, block)

override fun toString(): String = toStringInternalImpl() ?: delegate.toString()
}
32 changes: 32 additions & 0 deletions kotlinx-coroutines-core/js/test/ImmediateDispatcherTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines

import kotlin.test.*

class ImmediateDispatcherTest : TestBase() {

@Test
fun testImmediate() = runTest {
expect(1)
val job = launch { expect(3) }
withContext(Dispatchers.Main.immediate) {
expect(2)
}
job.join()
finish(4)
}

@Test
fun testMain() = runTest {
expect(1)
val job = launch { expect(2) }
withContext(Dispatchers.Main) {
expect(3)
}
job.join()
finish(4)
}
}
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/jvm/src/Executors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlin.coroutines.*
* Instances of [ExecutorCoroutineDispatcher] should be closed by the owner of the dispatcher.
*
* This class is generally used as a bridge between coroutine-based API and
* asynchronous API which requires instance of the [Executor].
* asynchronous API that requires an instance of the [Executor].
*/
public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closeable {
/** @suppress */
Expand Down
8 changes: 0 additions & 8 deletions kotlinx-coroutines-core/native/src/Dispatchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@ package kotlinx.coroutines
import kotlin.coroutines.*

public actual object Dispatchers {

public actual val Default: CoroutineDispatcher = createDefaultDispatcher()

public actual val Main: MainCoroutineDispatcher = NativeMainDispatcher(Default)

public actual val Unconfined: CoroutineDispatcher get() = kotlinx.coroutines.Unconfined // Avoid freezing
}

private class NativeMainDispatcher(val delegate: CoroutineDispatcher) : MainCoroutineDispatcher() {

override val immediate: MainCoroutineDispatcher
get() = throw UnsupportedOperationException("Immediate dispatching is not supported on Native")

override fun dispatch(context: CoroutineContext, block: Runnable) = delegate.dispatch(context, block)

override fun isDispatchNeeded(context: CoroutineContext): Boolean = delegate.isDispatchNeeded(context)

override fun dispatchYield(context: CoroutineContext, block: Runnable) = delegate.dispatchYield(context, block)

override fun toString(): String = toStringInternalImpl() ?: delegate.toString()
}
4 changes: 0 additions & 4 deletions ui/kotlinx-coroutines-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import org.jetbrains.dokka.DokkaConfiguration.ExternalDocumentationLink
import org.jetbrains.dokka.gradle.DokkaTask
import java.net.URL

repositories {
google()
}

configurations {
create("r8")
}
Expand Down