KMP-NativeCoroutines icon indicating copy to clipboard operation
KMP-NativeCoroutines copied to clipboard

Add JS support

Open rickclephas opened this issue 2 years ago • 11 comments

JS and native share some limitations with the coroutines interop. The generic way of exposing callbacks could be applied to JS as well. Allowing the clients to decide on an appropriate "native coroutines implementation".

rickclephas avatar Nov 08 '21 19:11 rickclephas

Differences between Apple native and JS:

  • JS doesn't require freezing
  • JS doesn't have NSErrors
  • JS requires the public API to be annotated with @JsExport
  • The used CoroutineScope is probably different on JS then it is on Apple native.

Thanks @ankushg for the info!

rickclephas avatar Nov 08 '21 19:11 rickclephas

Hey, in our company we're planning a library that would compile to Android, iOS, and Web, and this would be very useful. Do you have an ETA on when Js support is planned? Alternatively, do you have an idea on how to use coroutines from Js temporarily? Maybe the simplest thing would be converting to RxJs?

KirkBushman avatar Feb 09 '22 16:02 KirkBushman

Hi! I don't really have an ETA for this at the moment and am currently not actively working on it to be honest 😅.

However I would love to support JS as well. It's just that I am not really that familiar with JS and Kotlin/JS, so I am kind of lacking insights into the issues/challenges in terms of coroutines interop.

For iOS/Swift the 2 main issues are cancellation support and the lack of generics on interfaces in ObjC. If I am not mistaken JS has the same issue with cancellation support but doesn't have the issue with generics, right?

By any chance do you know of a sample JS project that uses a shared Kotlin module?

Once we have a clear picture of the challenges it should be fairly easy to define a JavaScripty API to use coroutines from JS code 😄.

Just some notes:

  • KMP-NativeCoroutines already support multiplatform projects with JS targets, so you can still use it for iOS in such projects. Though currently the whole library is no-op on non Apple platforms.
  • The compiler plugin generates IR code so this is probably not going to work for the "old" JS compiler.

rickclephas avatar Feb 09 '22 19:02 rickclephas

Hey @rickclephas,

I have worked with Kotlin/Js before so maybe I can help out.

Js is radically different from the others platform, it's single-threaded by default on the browser, they are using Workers to multithread. If I'm not incorrect you should be able to cancel a job in RxJs, I think if mapped correctly it might be our best bet.

I have no Kotlin/Js project to share atm, but I imagine we can create a very simple sample project, with a number and 2 actions to increase or decrease the number shown on the screen. Following the BLoC pattern, the shared module would contain the shared code. We can use react to show a few widgets, and that's it. Shouldn't take long to build. Would you host it in the same repo?

As you say, once we see the challenges shouldn't be hard to get it right.

What do you say?

KirkBushman avatar Feb 10 '22 12:02 KirkBushman

Yeah that would be great! Adding it to this repo makes sense since a lot of the logic will be the same regardless of the platform. The biggest differences will be in the wrapper functions.

It would be great if we could add a react "client" to the sample (with the clock and random letters views).

rickclephas avatar Feb 10 '22 12:02 rickclephas

I can maybe provide a PR when I got time

KirkBushman avatar Feb 10 '22 12:02 KirkBushman

If it helps https://github.com/joreilly/PeopleInSpace has a Kotlin/JS client (along with Compose for Web one)....it's already using this library for Kotlin/Native clients.

joreilly avatar Feb 10 '22 13:02 joreilly

Thanks @joreilly!! But that is all in Kotlin, right? In that case you can just use the coroutines code directly, no need for any wrappers 😄. The challenge would be in using the shared Kotlin module from plain JS code.

rickclephas avatar Feb 10 '22 13:02 rickclephas

ah, good point :)

joreilly avatar Feb 10 '22 13:02 joreilly

Not sure if this is the right issue, but I was trying some experiments with wasmJs and get a build error with this package (using ALPHA-15 atm). Is this something that may be supported at some point? Or should it work now?

tylerwilson avatar Nov 06 '23 01:11 tylerwilson

@tylerwilson this issues was/is actually about full support for JS (currently we only support projects with the JS target). Anyway support for the Wasm target will be added once kotlinx.coroutines has added support for it. Please watch #146

rickclephas avatar Nov 06 '23 06:11 rickclephas