Created
June 1, 2019 03:48
-
-
Save gpeal/a9e6b802ab2d5846ae44bee173f34c6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T : Any?> execute( | |
fn: suspend () -> T, | |
onError: (suspend (Exception) -> Unit)? = null, | |
dispatcher: CoroutineDispatcher = Dispatchers.Default, | |
reducer: S.(Async<T>) -> S | |
) { | |
uiScope.launch(dispatcher) { | |
setState { reducer(Loading()) } | |
try { | |
val result = fn() | |
setState { reducer(Success(result)) } | |
} catch (e: CancellationException) { | |
throw e | |
} catch (e: Exception) { | |
onError?.invoke(e) | |
setState { reducer(Fail(e)) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment