mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-03 16:10:00 +00:00
+6
-3
@@ -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()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user