Skip to content

Commit 189e81c

Browse files
committed
Added CoroutineScopeExt.kt
1 parent 96b8238 commit 189e81c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @created: 11 May 2021.
3+
* @author: Weiyi Li
4+
* Copyright (c) 2021 EROAD Limited. All rights reserved.
5+
*/
6+
package me.li2.android.common.kotlinx
7+
8+
import kotlinx.coroutines.CoroutineScope
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.Job
11+
import kotlinx.coroutines.launch
12+
13+
fun CoroutineScope.io(
14+
onError: (Exception) -> Unit = {},
15+
block: suspend CoroutineScope.() -> Unit
16+
): Job {
17+
return launch(Dispatchers.IO) {
18+
try {
19+
block()
20+
} catch (exception: Exception) {
21+
onError(exception)
22+
}
23+
}
24+
}
25+
26+
fun CoroutineScope.computation(
27+
onError: (Exception) -> Unit = {},
28+
block: suspend CoroutineScope.() -> Unit
29+
): Job {
30+
return launch(Dispatchers.IO) {
31+
try {
32+
block()
33+
} catch (exception: Exception) {
34+
onError(exception)
35+
}
36+
}
37+
}
38+
39+
fun <T> CoroutineScope.ui(block: CoroutineScope.() -> T): Job {
40+
return launch(Dispatchers.Main) {
41+
block()
42+
}
43+
}

0 commit comments

Comments
 (0)