File tree 2 files changed +41
-9
lines changed
kotlinx-coroutines-core/common/src
ui/kotlinx-coroutines-android/src
2 files changed +41
-9
lines changed Original file line number Diff line number Diff line change @@ -14,13 +14,30 @@ public abstract class MainCoroutineDispatcher : CoroutineDispatcher() {
14
14
15
15
/* *
16
16
* 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
+ *
19
37
* Method may throw [UnsupportedOperationException] if immediate dispatching is not supported by current dispatcher,
20
38
* please refer to specific dispatcher documentation.
21
39
*
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 .
23
41
*/
24
- @ExperimentalCoroutinesApi
25
42
public abstract val immediate: MainCoroutineDispatcher
26
43
}
Original file line number Diff line number Diff line change @@ -21,13 +21,28 @@ import kotlin.coroutines.*
21
21
*/
22
22
public sealed class HandlerDispatcher : MainCoroutineDispatcher (), Delay {
23
23
/* *
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 .
27
27
*
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
+ * ```
29
45
*/
30
- @ExperimentalCoroutinesApi
31
46
public abstract override val immediate: HandlerDispatcher
32
47
}
33
48
You can’t perform that action at this time.
0 commit comments