@@ -7,13 +7,10 @@ import kotlinx.coroutines.DelicateCoroutinesApi
77import kotlinx.coroutines.GlobalScope
88import kotlinx.coroutines.Job
99import kotlinx.coroutines.async
10- import kotlinx.coroutines.delay
1110import kotlinx.coroutines.launch
1211import kotlinx.coroutines.supervisorScope
1312import kotlin.coroutines.CoroutineContext
1413import kotlin.coroutines.EmptyCoroutineContext
15- import kotlin.time.Duration.Companion.milliseconds
16- import kotlin.time.measureTimedValue
1714
1815/* *
1916 * Launches a new coroutine in the [GlobalScope] without blocking the current thread and returns a reference to the coroutine as a [Job].
@@ -38,6 +35,20 @@ fun launchGlobal(
3835 return GlobalScope .launch(context, start, block)
3936}
4037
38+ /* *
39+ * Launches a new coroutine in the current scope and returns a reference to the coroutine as a [Job].
40+ * The coroutine is cancelled when the resulting job is [cancelled][Job.cancel].
41+ *
42+ * If the coroutine throws an exception, it will be caught and passed to the [onCatch] function. The function will be called
43+ * with the exception as its argument.
44+ *
45+ * @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
46+ * @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
47+ * @param onCatch the function to call when the coroutine throws an exception. The function will be called
48+ * with the exception as its argument.
49+ * @param block the coroutine code which will be invoked in the context of the provided scope.
50+ * @return A [Job] representing the launched coroutine.
51+ */
4152fun CoroutineScope.launchTrying (
4253 context : CoroutineContext = EmptyCoroutineContext ,
4354 start : CoroutineStart = CoroutineStart .DEFAULT ,
@@ -49,6 +60,16 @@ fun CoroutineScope.launchTrying(
4960 block = block
5061)
5162
63+ /* *
64+ * Launches a new coroutine in the current scope that will be supervised by the [supervisorScope] function.
65+ * This means that if the coroutine throws an exception, it will be propagated to the parent coroutine and
66+ * will not cancel the parent coroutine.
67+ *
68+ * @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
69+ * @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
70+ * @param block the coroutine code which will be invoked in the context of the provided scope.
71+ * @return A [Job] representing the launched coroutine.
72+ */
5273fun CoroutineScope.launchSupervise (
5374 context : CoroutineContext = EmptyCoroutineContext ,
5475 start : CoroutineStart = CoroutineStart .DEFAULT ,
@@ -58,7 +79,18 @@ fun CoroutineScope.launchSupervise(
5879}
5980
6081/* *
61- * Useful for view models.
82+ * A combination of [launchTrying] and [supervisorScope] that launches a coroutine in the current scope and
83+ * returns its result as a [kotlinx.coroutines.Deferred] value. If the coroutine throws an exception, it will be caught and
84+ * passed to the [onCatch] function.
85+ *
86+ * This function is useful for view models that need to handle exceptions in a centralized way.
87+ *
88+ * @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
89+ * @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
90+ * @param onCatch the function to call when the coroutine throws an exception. The function will be called
91+ * with the exception as its argument.
92+ * @param block the coroutine code which will be invoked in the context of the provided scope.
93+ * @return A [kotlinx.coroutines.Deferred] representing the launched coroutine.
6294 */
6395fun CoroutineScope.launchTryingSupervise (
6496 context : CoroutineContext = EmptyCoroutineContext ,
@@ -69,6 +101,20 @@ fun CoroutineScope.launchTryingSupervise(
69101 supervisorScope(block)
70102}
71103
104+ /* *
105+ * Launches a coroutine that will be cancelled when the resulting [kotlinx.coroutines.Deferred] is cancelled.
106+ * The coroutine is launched in the current scope and returns its result as a [kotlinx.coroutines.Deferred] value.
107+ * If the coroutine throws an exception, it will be caught and passed to the [onCatch] function.
108+ *
109+ * This function is useful for view models that need to handle exceptions in a centralized way.
110+ *
111+ * @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
112+ * @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
113+ * @param onCatch the function to call when the coroutine throws an exception. The function will be called
114+ * with the exception as its argument.
115+ * @param block the coroutine code which will be invoked in the context of the provided scope.
116+ * @return A [kotlinx.coroutines.Deferred] representing the launched coroutine.
117+ */
72118fun <T > CoroutineScope.asyncTrying (
73119 context : CoroutineContext = EmptyCoroutineContext ,
74120 start : CoroutineStart = CoroutineStart .DEFAULT ,
@@ -81,7 +127,18 @@ fun <T> CoroutineScope.asyncTrying(
81127)
82128
83129/* *
84- * Useful for view models.
130+ * A combination of [asyncTrying] and [supervisorScope] that launches a coroutine in the current scope and
131+ * returns its result as a [kotlinx.coroutines.Deferred] value. If the coroutine throws an exception, it will be caught and
132+ * passed to the [onCatch] function.
133+ *
134+ * This function is useful for view models that need to handle exceptions in a centralized way.
135+ *
136+ * @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
137+ * @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
138+ * @param onCatch the function to call when the coroutine throws an exception. The function will be called
139+ * with the exception as its argument.
140+ * @param block the coroutine code which will be invoked in the context of the provided scope.
141+ * @return A [kotlinx.coroutines.Deferred] representing the launched coroutine.
85142 */
86143fun <T > CoroutineScope.asyncTryingSupervise (
87144 context : CoroutineContext = EmptyCoroutineContext ,
@@ -90,17 +147,4 @@ fun <T> CoroutineScope.asyncTryingSupervise(
90147 block : suspend CoroutineScope .() -> T
91148) = asyncTrying(context, start, onCatch) {
92149 supervisorScope(block)
93- }
94-
95- suspend inline fun <T > runForAtLeast (
96- block : () -> T ,
97- durationInMillis : Long
98- ): T {
99- val (result, executionDuration) = measureTimedValue(block)
100-
101- if (executionDuration >= durationInMillis.milliseconds) {
102- delay(durationInMillis.milliseconds - executionDuration)
103- }
104-
105- return result
106150}
0 commit comments