What are the best practices for using coroutines i...
# multiplatform
c
What are the best practices for using coroutines in multiplatform?
l
Your question is too broad to be answer in chat. What do you miss exactly?
c
Not really looking for a specific, but more an abstract answer. I'm currently working on a jvm / js mpp where I'm trying to recreate redux and redux saga with coroutines. I'm putting all my async logic in the common and using it in js. But I'm student so kinda insecure about the choices I'm making atm.
l
I'm not familiar with redux, but I can tell you that MVI works well with suspending functions, regular functions, data classes and flows to abstract UI away. There's no state of the art about that yet though. I'm personally still figuring things out in that area. One thing that gained concensus though, is to expose and use suspending functions whenever it makes sense, and keep using suspending functions as long as it makes sense: no need to wrap them, take advantage of them to abstract details and complexity away.
šŸ˜€ 1
c
As far as my understanding, Redux is a npm library which manages the state of an application. Redux Saga is an add-on to redux which takes generator functions and have them execute asynchronous. I've tried to resemble these two architectures in kotlin by creating enum and data classes to manage the state and create a single suspending function which fires a coroutine with an "action" from the Action store (which is filled with functions that manipulate the enum and data classes). It works well, but I was in a lot of doubt about it, because it worked..
e
I’m currently looking at implementing ā€œreactive patternsā€ in Kotlin MPP. It’s based off of work I’ve done with MVI. I’m at the stage where I have a similar ā€œgeneral questioningā€ going on. I.e. AFAIK Multi threaded coroutines are not available on KN, so how should I handle async work (like, say, a simple web call)
My understanding is that I’ll prob ably be able to get what I want from the existing primitives, but my goal is to unify approaches between JVM / Native
l
@Etienne You can use CoroutineWorker by Autodesk or make your own version until coroutines dispatcher switching becomes available right in kotlinx.coroutines for Kotlin/Native.
Also, ktor http client works asychronously on the main thread, so you can do all suspending functions already. And there's a main thread dispatcher for iOS somewhere on the web, Google should find it quick
e
šŸ‘ thank you Louis! šŸ™‚