@@ -17,32 +17,50 @@ package com.fernandocejas.sample.core.interactor
17
17
18
18
import com.fernandocejas.sample.core.failure.Failure
19
19
import com.fernandocejas.sample.core.functional.Either
20
- import kotlinx.coroutines.*
20
+ import kotlinx.coroutines.CoroutineScope
21
+ import kotlinx.coroutines.Dispatchers
22
+ import kotlinx.coroutines.MainScope
23
+ import kotlinx.coroutines.async
24
+ import kotlinx.coroutines.launch
21
25
22
26
/* *
23
- * Abstract class for a Use Case (Interactor in terms of Clean Architecture).
24
- * This abstraction represents an execution unit for different use cases (this means that any use
25
- * case in the application should implement this contract).
27
+ * Abstract class for a Use Case (Interactor in terms of
28
+ * Clean Architecture naming convention).
26
29
*
27
- * By convention each [UseCase] implementation will execute its job in a background thread
28
- * (kotlin coroutine) and will post the result in the UI thread.
30
+ * This abstraction represents an execution unit for
31
+ * different use cases (this means that any use case
32
+ * in the application should implement this contract).
33
+ *
34
+ * By convention each [UseCase] implementation will
35
+ * execute its job in a pool of threads using
36
+ * [Dispatchers.IO].
37
+ *
38
+ * The result of the computation will be posted on the
39
+ * same thread used by the @param 'scope' [CoroutineScope].
29
40
*/
30
41
abstract class UseCase <out Type , in Params > where Type : Any {
31
42
32
43
abstract suspend fun run (params : Params ): Either <Failure , Type >
33
44
34
45
operator fun invoke (
35
46
params : Params ,
36
- scope : CoroutineScope = GlobalScope ,
47
+ scope : CoroutineScope = MainScope () ,
37
48
onResult : (Either <Failure , Type >) -> Unit = {}
38
49
) {
39
- scope.launch( Dispatchers . Main ) {
40
- val deferred = async(Dispatchers .IO ) {
50
+ scope.launch {
51
+ val deferredJob = async(Dispatchers .IO ) {
41
52
run (params)
42
53
}
43
- onResult(deferred .await())
54
+ onResult(deferredJob .await())
44
55
}
45
56
}
46
57
58
+ /* *
59
+ * Helper class to represent Empty
60
+ * Params when a use case does not
61
+ * need them.
62
+ *
63
+ * @see UseCase
64
+ */
47
65
class None
48
66
}
0 commit comments