-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseCase.kt
More file actions
21 lines (18 loc) · 729 Bytes
/
Copy pathUseCase.kt
File metadata and controls
21 lines (18 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
abstract class UseCase<out Type, in Params> {
private var backgroundContext: CoroutineContext = Dispatchers.IO
private var foregroundContext: CoroutineContext = Dispatchers.Main
private var parentJob: Job = Job()
abstract suspend fun run(params: Params): Either<Failure, Type>
operator fun invoke(params: Params, onResult: (Either<Failure, Type>) -> Unit = {}) {
unsubscribe()
parentJob = Job()
CoroutineScope(foregroundContext + parentJob).launch {
val result = withContext(backgroundContext) { run(params) }
onResult(result)
}
}
fun unsubscribe() {
parentJob.cancelChildren()
parentJob.cancel()
}
}