Skip to content

Commit fcf6cbd

Browse files
authored
Simplified coroutine use in ContinuousExecutingUseCase. (#607)
1 parent e1c9af6 commit fcf6cbd

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

architecture/domain/src/main/java/com/mitteloupe/whoami/architecture/domain/usecase/ContinuousExecutingUseCase.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ import kotlinx.coroutines.CancellationException
55
import kotlinx.coroutines.CoroutineScope
66
import kotlinx.coroutines.Dispatchers
77
import kotlinx.coroutines.flow.Flow
8+
import kotlinx.coroutines.flow.flowOn
89
import kotlinx.coroutines.launch
9-
import kotlinx.coroutines.withContext
1010

1111
abstract class ContinuousExecutingUseCase<REQUEST, RESULT>(
1212
private val coroutineContextProvider: CoroutineContextProvider,
1313
private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Main)
1414
) : UseCase<REQUEST, RESULT> {
1515
final override fun execute(input: REQUEST, onResult: (RESULT) -> Unit) {
1616
try {
17-
coroutineScope.launch {
18-
withContext(coroutineContextProvider.io) {
19-
executeInBackground(input).collect { result ->
20-
withContext(coroutineContextProvider.main) {
21-
onResult(result)
22-
}
17+
coroutineScope.launch(coroutineContextProvider.main) {
18+
executeInBackground(input)
19+
.flowOn(coroutineContextProvider.io)
20+
.collect { result ->
21+
onResult(result)
2322
}
24-
}
2523
}
2624
} catch (_: CancellationException) {
2725
}

0 commit comments

Comments
 (0)