https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
m

Michal Klimczak

01/25/2021, 6:47 PM
I have created a codegen library which generates wrappers for coroutines and flows. Heavily inspired by @russhwolf's article about SuspendWrappers. This is an early preview, a lot will change. I'd appreciate your feedback and sincerely hope this will become useful for the community. https://github.com/FutureMind/kmm-ios-suspendwrapper
👍 8
r

russhwolf

01/25/2021, 6:59 PM
Neat!
❤️ 1
At some point I want to dig in and understand your scope handling better. That always felt like something that could use a bit of cleanup in the original post. I optimized the description in the post for maximum flexibility, but if you always use the same scope in practice then you can just have it live inside the
...Ios
wrapper class and don't need to pass it in externally.
m

Michal Klimczak

01/25/2021, 7:13 PM
At first I was planning to use a new scope for every suspendwrapper and handle cancelling them at the right moment. But I wasn't sure if I would not leak them in some scenario. Providing the same scope from the outside (with possibility to override it if necessary) seems like an good solution to me but I'm open for other ideas.
j

John O'Reilly

01/25/2021, 7:22 PM
Would love to see this or some variation of it become part of Kotlin/Native itself
💯 1
m

Michal Klimczak

01/25/2021, 7:24 PM
Yeah, the way that suspend functions are converted right now is far from flexible, no way to write an extension if there is no return type,but a completion handler as the last parameter instead.
a

ankushg

01/25/2021, 7:34 PM
Looks great! Do `SuspendWrapper`s or `FlowWrapper`s provide a way for iOS clients to cancel? I liked that aspect of the helpers exposed by the kotlinconf app: https://github.com/JetBrains/kotlinconf-app/blob/d56c3383d52dfdc32ba449d8daf9b9f63[…]mon/src/mobileMain/kotlin/org/jetbrains/kotlinconf/FlowUtils.kt
👍 1
m

Michal Klimczak

01/25/2021, 7:42 PM
It can be canceled via the provided scope, but then of course you need to provide it explicitly. This requires some more thought ;)
r

russhwolf

01/25/2021, 7:44 PM
I didn't look closely at it here but in my original article the
subscribe
calls return `Job`s that you can use to cancel, in addition to scopes.
j

John O'Reilly

01/25/2021, 7:44 PM
(not too surprisingly given it was also heavily based on @russhwolf’s code 🙂 )
r

russhwolf

01/25/2021, 7:50 PM
re built-ins, part of the issue is the language-level coroutine constructs don't expose cancellation. It only lives in the the kotlinx dependency. So I think the closest you'd ever get to a built-in for this stuff is a kotlinx-coroutines module that includes it (ideally including a compiler plugin for code-gen, something like this one)
1
a

ankushg

01/25/2021, 8:04 PM
Yeah it would be nice if the
subscribe
call returned an object to cancel the operation even in the automatically-provided-scope case I'm not sure if exposing a full
Job
is the right approach or not -- it looks like JB just return an implementation of
Closeable
that calls
Job.cancel
behind the scenes Regardless, this is a super cool proof of concept 😄
m

Michal Klimczak

01/25/2021, 8:09 PM
Oh, wait, I forgot that you can cancel this way. This is almost copied from @russhwolf solution. It returns a Job and it can be canceled that way of course, not just via scope. https://github.com/FutureMind/kmm-ios-suspendwrapper/blob/master/ioswrapper-annotation/src/commonMain/kotlin/com/futuremind/iossuspendwrapper/SuspendWrapper.kt I even do that in my Swift code where I make Combine Publisher from SuspendWrapper, will provide more docs on that.
a

ankushg

01/25/2021, 8:13 PM
Oh nice! Looking forward to seeing an example of this in action
👍 1
a

Andrea Prearo

01/25/2021, 8:32 PM
It’d be great to have a standardized way to consume coroutines and Kotlin Flows in iOS. So far, this has been a tricky aspect of working with KMP. I agree with @John O'Reilly that something like this should be provided as part of Kotlin Native itself.
m

Michal Klimczak

01/25/2021, 8:34 PM
Not sure about kotlin itself, but Android has a long history of incorporating community solutions. And KMM / KMP evolves rapidly, so who knows
👍 1
6 Views