inside something that’s not entry-programmy like an event callback, there’s a way of doing it through a
Promise
or a
Channel
.
pakoito
04/14/2020, 11:47 PM
Promise/Queue still require calling unsafeRunAsync to set a value. Still, they decouple your business logic from the event handler, and makes it easier to test.
pakoito
04/14/2020, 11:50 PM
instead of
Copy code
system.handleEvent {
val bla = run(it).unsafeRunSync() // block thread
continue(bla) // side-effect outside IO
}
you have
Copy code
IO.fx {
continueOn(dispatchers().default()) // process on the background
val next = queue.take().bind()
val bla = run(it).bind()
effect { continue(bla) }.bind()
}.unsafeRunAsync { } // immediately free the thread
system.handleEvent {
queue.offer(it)
.fork() // offer the value on the background
.unsafeRunAsync { } // immediately free the thread
}
pakoito
04/14/2020, 11:55 PM
if instead of that you are at a place that supports suspension, then