parent
7b84a7d8fe
commit
924103521f
1 changed files with 6 additions and 3 deletions
|
|
@ -36,17 +36,20 @@ sealed interface Async<out T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun <T> (suspend () -> T).execute(state: MutableState<Async<T>>, errorMapping: ((Throwable) -> Throwable)? = null) {
|
suspend inline fun <T> (suspend () -> T).execute(
|
||||||
|
state: MutableState<Async<T>>,
|
||||||
|
errorMapping: ((Throwable) -> Throwable) = { it },
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
state.value = Async.Loading()
|
state.value = Async.Loading()
|
||||||
val result = this()
|
val result = this()
|
||||||
state.value = Async.Success(result)
|
state.value = Async.Success(result)
|
||||||
} catch (error: Throwable) {
|
} catch (error: Throwable) {
|
||||||
state.value = Async.Failure(errorMapping?.invoke(error) ?: error)
|
state.value = Async.Failure(errorMapping.invoke(error))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun <T> (suspend () -> Result<T>).executeResult(state: MutableState<Async<T>>) {
|
suspend inline fun <T> (suspend () -> Result<T>).executeResult(state: MutableState<Async<T>>) {
|
||||||
if (state.value !is Async.Success) {
|
if (state.value !is Async.Success) {
|
||||||
state.value = Async.Loading()
|
state.value = Async.Loading()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue