forked from github/wulkanowy-mirror
Fix crash in flowWithResourceIn() (#933)
This commit is contained in:
parent
a5de39a366
commit
eb616eedc7
@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.catch
|
|||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.lang.NullPointerException
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
import java.time.LocalDate.now
|
import java.time.LocalDate.now
|
||||||
import java.time.LocalDate.of
|
import java.time.LocalDate.of
|
||||||
|
@ -3,6 +3,7 @@ package io.github.wulkanowy.utils
|
|||||||
import io.github.wulkanowy.data.Resource
|
import io.github.wulkanowy.data.Resource
|
||||||
import io.github.wulkanowy.data.Status
|
import io.github.wulkanowy.data.Status
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.emitAll
|
import kotlinx.coroutines.flow.emitAll
|
||||||
import kotlinx.coroutines.flow.filter
|
import kotlinx.coroutines.flow.filter
|
||||||
@ -69,22 +70,24 @@ inline fun <ResultType, RequestType, T> networkBoundResource(
|
|||||||
|
|
||||||
fun <T> flowWithResource(block: suspend () -> T) = flow {
|
fun <T> flowWithResource(block: suspend () -> T) = flow {
|
||||||
emit(Resource.loading())
|
emit(Resource.loading())
|
||||||
try {
|
emit(try {
|
||||||
emit(Resource.success(block()))
|
Resource.success(block())
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
emit(Resource.error(e))
|
Resource.error(e)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> flowWithResourceIn(block: suspend () -> Flow<Resource<T>>) = flow {
|
fun <T> flowWithResourceIn(block: suspend () -> Flow<Resource<T>>) = flow {
|
||||||
emit(Resource.loading())
|
emit(Resource.loading())
|
||||||
|
|
||||||
try {
|
try {
|
||||||
block().collect {
|
block()
|
||||||
if (it.status != Status.LOADING) { // LOADING is already emitted
|
.catch { emit(Resource.error(it)) }
|
||||||
emit(it)
|
.collect {
|
||||||
|
if (it.status != Status.LOADING) { // LOADING is already emitted
|
||||||
|
emit(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
emit(Resource.error(e))
|
emit(Resource.error(e))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user