Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlinx.coroutines.CoroutineScope
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.launch
- import kotlinx.coroutines.withContext
- class ResultAsync<T> private constructor( scope: CoroutineScope, action: suspend () -> T){
- var onSuccess : (T) -> Unit = {}
- var onError : (e: Throwable) -> Unit = {}
- var onStatusChange : (status: AsyncStatus) -> Unit = {}
- companion object {
- fun <T> with( scope: CoroutineScope, action: suspend () -> T) : ResultAsync<T> {
- return ResultAsync(scope, action)
- }
- }
- init {
- scope.launch {
- withContext(Dispatchers.Main) { onStatusChange(AsyncStatus.RUNNING) }
- try {
- val result = action()
- withContext(Dispatchers.Main){
- onStatusChange(AsyncStatus.DONE)
- onSuccess(result)
- }
- }catch (e:Throwable){
- withContext(Dispatchers.Main){
- onStatusChange(AsyncStatus.ERROR)
- onError(e)
- }
- }
- }
- }
- }
- fun <T> ResultAsync<T>.onFailure(action: (exception: Throwable) -> Unit): ResultAsync<T> {
- this.onError = action
- return this
- }
- fun <T> ResultAsync<T>.onSuccess(action: (value: T) -> Unit): ResultAsync<T> {
- this.onSuccess = action
- return this
- }
- fun <T> ResultAsync<T>.onStatusChange(action: (AsyncStatus) -> Unit): ResultAsync<T> {
- this.onStatusChange = action
- return this
- }
- fun <T> CoroutineScope.asyncCatching( action: suspend () -> T): ResultAsync<T> {
- return ResultAsync.with( this,action)
- }
- enum class AsyncStatus{
- RUNNING,
- DONE,
- ERROR
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement