Hi! I don't know yet the abstractions in arrow to achieve what I'm trying to do. Basically, I'd like to do some switchMapping. Here's a very simple example of what I'm trying to do:
fun <T, E> IO<T>.setState(handleError: (Throwable) -> E, onSuccess: (T) -> IO<Any>) = IO.fx {
val state = !this@setState.attempt()
state.fold(
{ handleError(it) },
{
// Other stuff happening…
!onSuccess(it)
}
)
}
@Test
fun test() {
val handleError = { t: Throwable -> println("Error: ${t.message}") }
val onSuccess = { bool: Boolean ->
IO.fx {
println("Starting")
!effect { delay(5000) } // --> SwitchMapping should happen around here
println("Finished with value $bool")
}
}
// Here, this is what I don't know how to "Observablify" to send multiple events
IO.just(true).setState(handleError, onSuccess).unsafeRunAsync {}
IO.just(false).setState(handleError, onSuccess).unsafeRunSync()
}
What I expect is:
Starting
Starting
Finished with value false // The other one should be "switchMapped"
I don't know how to have some kind of flow or observable with arrow, and I don't know how to switch map within it. Any guidance?