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

Restart scala.js runtime environment #1959

Closed
cquiroz opened this issue May 12, 2021 · 6 comments
Closed

Restart scala.js runtime environment #1959

cquiroz opened this issue May 12, 2021 · 6 comments
Assignees
Milestone

Comments

@cquiroz
Copy link

cquiroz commented May 12, 2021

We build all our scala.js apps with cats-effect 3 starting with IOApp which is exported to the js runtime. e.g.
https://github.com/gemini-hlsw/explore/blob/fef6caaafdafa144b6a9ae04afad18990467e132/explore/src/main/scala/explore/Explore.scala#L61

This works fine for the regular browser case and node but has some restrictions when doing development on browser.
There we often want to use HMR (Hot module replacement) where the dev environent detects changes to the compiled scala.js output and reloads the js code without a browser reload.

In practice this means we call IOApp.main again, however the code below can only be ever called once.
https://github.com/typelevel/cats-effect/blob/series/3.x/core/js/src/main/scala/cats/effect/IOApp.scala#L33

Would it be possible to add a method that could be called on replacement to reset the environment such that another call to IOApp.main wouldn't fail?

@djspiewak
Copy link
Member

This is very interesting! How would you expect this to work exactly? Just an IOApp#reset() or something like that?

@cquiroz
Copy link
Author

cquiroz commented May 24, 2021

Yes! a method on IOApp that could be called from the JS runtime

@djspiewak
Copy link
Member

Do you just want it exposed to JavaScript, or is this something you would call from Scala?

@cquiroz
Copy link
Author

cquiroz commented May 24, 2021

I wouldn't expect to call it from java on my use case of HMR. And it would need to be a kind of end-of-the-world call returning Unit to be exported easily to js

@djspiewak
Copy link
Member

I think this is sufficiently addressed as of #2114. If you disagree, leave a comment and I'll reopen!

@armanbilge
Copy link
Member

So I wasn't around when this originally came up, but I think the right solution here is that browser applications that have bespoke needs just shouldn't use IOApp. Instead they can just call unsafeRunAndForget().

IOApp works fine for a vanilla usecase, but any serious web framework has its own application class anyway. IOApp brings little to no value for browser applications.

Calico offers an IOWebApp:
https://github.com/armanbilge/calico/blob/bc9b5cb9b06708c0a2fc35cc66c593823445d8f3/calico/src/main/scala/calico/IOWebApp.scala#L25

Tyrian offers a TyrianApp:
https://github.com/PurpleKingdomGames/tyrian/blob/80a6b76944309a98048d97dc4709154195ac2bac/tyrian-io/src/main/scala/tyrian/TyrianApp.scala#L15

Indeed, even the application that prompted this issue is simply exporting IOApp#main to be called from JavaScript. Which is all rather silly :)

https://github.com/gemini-hlsw/explore/blob/fef6caaafdafa144b6a9ae04afad18990467e132/explore/src/main/scala/explore/Explore.scala#L60-L61

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

No branches or pull requests

3 participants