Skip to content

Commit 9942de6

Browse files
committed
Promote MainCoroutineDispatcher.immediate to stable API
1 parent 7590b05 commit 9942de6

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

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

+21-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,30 @@ public abstract class MainCoroutineDispatcher : CoroutineDispatcher() {
1414

1515
/**
1616
* Returns dispatcher that executes coroutines immediately when it is already in the right context
17-
* (e.g. current looper is the same as this handler's looper). See [isDispatchNeeded] documentation on
18-
* why this should not be done by default.
17+
* (e.g. current looper is the same as this handler's looper) without an additional [re-dispatch][CoroutineDispatcher.dispatch].
18+
*
19+
* Immediate dispatcher is safe from stack overflows and in case of nested invocations forms event-loop similar to [Dispatchers.Unconfined].
20+
* The event loop is an advanced topic and its implications can be found in [Dispatchers.Unconfined] documentation.
21+
*
22+
* Example of usage:
23+
* ```
24+
* suspend fun updateUiElement(val text: String) {
25+
* /*
26+
* * If it is known that updateUiElement can be invoked both from the Main thread and from other threads,
27+
* * `immediate` dispatcher is used as a performance optimization to avoid unnecessary dispatch.
28+
* */
29+
* withContext(Dispatchers.Main.immediate) {
30+
* uiElement.text = text
31+
* }
32+
* // Do context-independent logic such as logging
33+
* }
34+
*
35+
* ```
36+
*
1937
* Method may throw [UnsupportedOperationException] if immediate dispatching is not supported by current dispatcher,
2038
* please refer to specific dispatcher documentation.
2139
*
22-
* **Note: This is an experimental api.** Semantics of this dispatcher may change in the future.
40+
* [Dispatchers.Main] supports immediate execution for Android, JavaFx and Swing platforms.
2341
*/
24-
@ExperimentalCoroutinesApi
2542
public abstract val immediate: MainCoroutineDispatcher
2643
}

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,28 @@ import kotlin.coroutines.*
2121
*/
2222
public sealed class HandlerDispatcher : MainCoroutineDispatcher(), Delay {
2323
/**
24-
* Returns dispatcher that executes coroutines immediately when it is already in the right handler context
25-
* (current looper is the same as this handler's looper). See [isDispatchNeeded] documentation on
26-
* why this should not be done by default.
24+
* Returns dispatcher that executes coroutines immediately when it is already in the right context
25+
* (current looper is the same as this handler's looper) without an additional [re-dispatch][CoroutineDispatcher.dispatch].
26+
* This dispatcher does not use [Handler.post] when current looper is the same as looper of the handler.
2727
*
28-
* **Note: This is an experimental api.** Semantics of this dispatcher may change in the future.
28+
* Immediate dispatcher is safe from stack overflows and in case of nested invocations forms event-loop similar to [Dispatchers.Unconfined].
29+
* The event loop is an advanced topic and its implications can be found in [Dispatchers.Unconfined] documentation.
30+
*
31+
* Example of usage:
32+
* ```
33+
* suspend fun updateUiElement(val text: String) {
34+
* /*
35+
* * If it is known that updateUiElement can be invoked both from the Main thread and from other threads,
36+
* * `immediate` dispatcher is used as a performance optimization to avoid unnecessary dispatch.
37+
* */
38+
* withContext(Dispatchers.Main.immediate) {
39+
* uiElement.text = text
40+
* }
41+
* // Do context-independent logic such as logging
42+
* }
43+
*
44+
* ```
2945
*/
30-
@ExperimentalCoroutinesApi
3146
public abstract override val immediate: HandlerDispatcher
3247
}
3348

0 commit comments

Comments
 (0)