Skip to content

Commit c81dc91

Browse files
authored
Documentation improvements (#1229)
* Improve MainCoroutineDispatcher.immediate documentation * Remove obsolete readme and package descriptions * Clarify possible replacement of onCompletion parameter in produce * Add paragraph about parent cancellation to async (Mention structured concurrency, but do not explain it there as the term should be easily googlable). Fixes #1219 Fixes #994 Fixes #787
1 parent 9b05908 commit c81dc91

File tree

8 files changed

+24
-132
lines changed

8 files changed

+24
-132
lines changed

kotlinx-coroutines-core/README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Module kotlinx-coroutines-core
22

3-
Core primitives to work with coroutines, available on all platforms.
3+
Core primitives to work with coroutines.
44

55
Coroutine builder functions:
66

@@ -84,13 +84,9 @@ Select expression to perform multiple suspending operations simultaneously until
8484

8585
Low-level primitives for finer-grained control of coroutines.
8686

87-
# Package kotlinx.coroutines.timeunit
88-
89-
Optional time unit support for multiplatform projects.
90-
9187
# Package kotlinx.coroutines.test
9288

93-
Components to ease writing unit-tests for code that contains coroutines with delays and timeouts.
89+
Obsolete and deprecated module to test coroutines. Replaced with `kotlinx-coroutines-test` module.
9490

9591
<!--- MODULE kotlinx-coroutines-core -->
9692
<!--- INDEX kotlinx.coroutines -->

kotlinx-coroutines-core/common/README.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Module kotlinx-coroutines-core
22

3-
Core primitives to work with coroutines.
3+
Core primitives to work with coroutines available on all platforms.
44

55
Coroutine builder functions:
66

@@ -10,7 +10,6 @@ Coroutine builder functions:
1010
| [async] | [Deferred] | [CoroutineScope] | Returns a single value with the future result
1111
| [produce][kotlinx.coroutines.channels.produce] | [ReceiveChannel][kotlinx.coroutines.channels.ReceiveChannel] | [ProducerScope][kotlinx.coroutines.channels.ProducerScope] | Produces a stream of elements
1212
| [actor][kotlinx.coroutines.channels.actor] | [SendChannel][kotlinx.coroutines.channels.SendChannel] | [ActorScope][kotlinx.coroutines.channels.ActorScope] | Processes a stream of messages
13-
| [runBlocking] | `T` | [CoroutineScope] | Blocks the thread while the coroutine runs
1413

1514
Coroutine dispatchers implementing [CoroutineDispatcher]:
1615

@@ -96,22 +95,13 @@ Select expression to perform multiple suspending operations simultaneously until
9695

9796
Low-level primitives for finer-grained control of coroutines.
9897

99-
# Package kotlinx.coroutines.timeunit
100-
101-
Optional time unit support for multiplatform projects.
102-
103-
# Package kotlinx.coroutines.test
104-
105-
Components to ease writing unit-tests for code that contains coroutines with delays and timeouts.
106-
10798
<!--- MODULE kotlinx-coroutines-core -->
10899
<!--- INDEX kotlinx.coroutines -->
109100
[launch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html
110101
[Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html
111102
[CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html
112103
[async]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html
113104
[Deferred]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/index.html
114-
[runBlocking]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html
115105
[CoroutineDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html
116106
[Dispatchers.Default]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html
117107
[Dispatchers.Unconfined]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-unconfined.html

kotlinx-coroutines-core/common/src/Builders.common.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public fun CoroutineScope.launch(
6060
/**
6161
* Creates a coroutine and returns its future result as an implementation of [Deferred].
6262
* The running coroutine is cancelled when the resulting deferred is [cancelled][Job.cancel].
63+
* The resulting coroutine has a key difference compared with similar primitives in other languages
64+
* and frameworks: it cancels the parent job (or outer scope) on failure to enforce *structured concurrency* paradigm.
65+
* To change that behaviour, supervising parent ([SupervisorJob] or [supervisorScope]) can be used.
6366
*
6467
* Coroutine context is inherited from a [CoroutineScope], additional context elements can be specified with [context] argument.
6568
* If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used.
@@ -72,8 +75,6 @@ public fun CoroutineScope.launch(
7275
* the resulting [Deferred] is created in _new_ state. It can be explicitly started with [start][Job.start]
7376
* function and will be started implicitly on the first invocation of [join][Job.join], [await][Deferred.await] or [awaitAll].
7477
*
75-
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
76-
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
7778
* @param block the coroutine code.
7879
*/
7980
public fun <T> CoroutineScope.async(

kotlinx-coroutines-core/common/src/MainCoroutineDispatcher.kt

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public abstract class MainCoroutineDispatcher : CoroutineDispatcher() {
2525
* /*
2626
* * If it is known that updateUiElement can be invoked both from the Main thread and from other threads,
2727
* * `immediate` dispatcher is used as a performance optimization to avoid unnecessary dispatch.
28+
* *
29+
* * In that case, when `updateUiElement` is invoked from the Main thread, `uiElement.text` will be
30+
* * invoked immediately without any dispatching, otherwise, the `Dispatchers.Main` dispatch cycle will be triggered.
2831
* */
2932
* withContext(Dispatchers.Main.immediate) {
3033
* uiElement.text = text

kotlinx-coroutines-core/common/src/channels/Produce.kt

+10-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,16 @@ public fun <E> CoroutineScope.produce(
100100
}
101101

102102
/**
103-
* @suppress **This an internal API and should not be used from general code.**
104-
* onCompletion parameter will be redesigned.
103+
* This an internal API and should not be used from general code.**
104+
* onCompletion parameter will be redesigned.
105+
* If you have to use `onCompletion` operator, please report to https://github.com/Kotlin/kotlinx.coroutines/issues/.
106+
* As a temporary solution, [invokeOnCompletion][Job.invokeOnCompletion] can be used instead:
107+
* ```
108+
* fun <E> ReceiveChannel<E>.myOperator(): ReceiveChannel<E> = GlobalScope.produce(Dispatchers.Unconfined) {
109+
* coroutineContext[Job]?.invokeOnCompletion { consumes() }
110+
* }
111+
* ```
112+
* @suppress
105113
*/
106114
@InternalCoroutinesApi
107115
public fun <E> CoroutineScope.produce(

kotlinx-coroutines-core/native/README.md

-111
This file was deleted.

kotlinx-coroutines-test/src/TestCoroutineExceptionHandler.kt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class TestCoroutineExceptionHandler :
4040
{
4141
private val _exceptions = mutableListOf<Throwable>()
4242

43+
/** @suppress **/
4344
override fun handleException(context: CoroutineContext, exception: Throwable) {
4445
synchronized(_exceptions) {
4546
_exceptions += exception

ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public sealed class HandlerDispatcher : MainCoroutineDispatcher(), Delay {
3434
* /*
3535
* * If it is known that updateUiElement can be invoked both from the Main thread and from other threads,
3636
* * `immediate` dispatcher is used as a performance optimization to avoid unnecessary dispatch.
37+
* *
38+
* * In that case, when `updateUiElement` is invoked from the Main thread, `uiElement.text` will be
39+
* * invoked immediately without any dispatching, otherwise, the `Dispatchers.Main` dispatch cycle via
40+
* * `Handler.post` will be triggered.
3741
* */
3842
* withContext(Dispatchers.Main.immediate) {
3943
* uiElement.text = text

0 commit comments

Comments
 (0)