Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JS support #17

Open
rickclephas opened this issue Nov 8, 2021 · 11 comments
Open

Add JS support #17

rickclephas opened this issue Nov 8, 2021 · 11 comments

Comments

@rickclephas
Copy link
Owner

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 rickclephas self-assigned this Nov 8, 2021
@rickclephas
Copy link
Owner Author

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!

@KirkBushman
Copy link

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?

@rickclephas
Copy link
Owner Author

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.

@KirkBushman
Copy link

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?

@rickclephas
Copy link
Owner Author

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).

@KirkBushman
Copy link

I can maybe provide a PR when I got time

@joreilly
Copy link

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.

@rickclephas
Copy link
Owner Author

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.

@joreilly
Copy link

ah, good point :)

@tylerwilson
Copy link

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?

@rickclephas
Copy link
Owner Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants