简体   繁体   中英

Arrow.kt loading state

I used arrow.kt library so many times, I really enjoyed the features they gave to extend kotlin. I like how Either<E,T> can represent the success/failed states seamlessly. I am just wondering if arrow.kt has a way to represent loading state along with the other two. In other word, we will have a type that offers three states: Success , Failure , Loading .

I tried to use Option<T> as right member, but None can't represent loading state as required.

Is there any thing I can try?

Either<A, B> represents a single result. You are interested in either the left side or the right side. There's no third. You could play it in many ways.

  1. For example, consider both Failure and Success as a right result.
sealed interface State {
    object Loading: State

    sealed interface Result: State {
        object Failure: Result
        object Success: Result
    }
}

//...
getState().map { result -> 
    when(result) {
        Failure -> TODO("Handle error")
        Success -> TODO("Handle success")
    }
}.mapLeft { loading -> TODO() }

and return Either<Loading, Result> .

  1. Or have Either<Loading, Either<Failure, Success>>

And it is even possible that some kind of subscription to Flow<State> or rx Observable will suit you more than a single result if you expect to receive multiple responses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM