Hello, I'm trying to achieve something similar to ...
# arrow
k
Hello, I'm trying to achieve something similar to this using Arrow:
Copy code
abstract class UseCase<out Type, in Params> where Type : Any {
    abstract suspend fun run(params: Params): Either<Failure, Type>

    fun execute(onResult: (Either<Failure, Type>) -> Unit, params: Params) {
        val job = GlobalScope.async(<http://Dispatchers.IO|Dispatchers.IO>) { run(params) }
        GlobalScope.launch(Dispatchers.Main) { onResult(job.await()) }
    }
}
Shamelessly trying to pick stuff from @Jorge Castillo's ArrowAndroidSamples, I came up with an fx version looking like this:
Copy code
fun <Type> IO<Type>.useCase(ok: (Type) -> Unit, error: (Throwable) -> Type): IO<Unit> = fx { this@useCase.handleError(error).map(ok).bind() }
Here's a concrete example for this in my tests:
Copy code
storeRepository.getStores().useCase({ stores -> println("stores: $stores") }, { t -> println("Ouch: $t"); emptyList() }).unsafeRunSync()
There's a couple of things I don't understand: - In case of an error, my output is:
Copy code
Ouch: IllegalArgumentException
stores: []
This is due to the fact that my error lambda has to return a List<Store> so it can be chained with the
map
. How can I avoid this? - I don't see where I can put continuations so that my network call gets done on IO and my error and ok handling gets done on the Main thread. Any idea?