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

Ema JavaScript API #69

Open
1 of 2 tasks
srid opened this issue Jan 29, 2022 · 7 comments
Open
1 of 2 tasks

Ema JavaScript API #69

srid opened this issue Jan 29, 2022 · 7 comments
Labels
enhancement New feature or request
Milestone

Comments

@srid
Copy link
Owner

srid commented Jan 29, 2022

Allow users to 'control' Ema's live server through WebSocket. Add a window.ema object containing the API endpoints.

Use cases:

@applejag
Copy link

applejag commented Jul 28, 2022

Could be nice with sending hot-reload events, such as:

window.dispatchEvent(new Event('EMABeforeDOMMorph'));
// do DOM morph
window.dispatchEvent(new Event('EMABeforeScriptReload'));
// do script reload
window.dispatchEvent(new Event('EMAHotReload'));

@applejag
Copy link

applejag commented Jul 31, 2022

Would be useful to exclude some scripts from being force-reloaded, such as:

<script data-ema-reload="never">

With values for the data-ema-reload attribute being:

  • auto (default) uses always on <script> and diff on anything else
  • always reloads script on every hot-reload
  • diff only reload script if the script tag changed by morphdom
  • never skip reloading this script on hot-reloads, including skipping morphdom

The diff and never could maybe be supported on any arbitrary HTML tag as well to make morphdom ignore certain node trees, such as stopping morphdom from removing JS-created elements.

@srid
Copy link
Owner Author

srid commented Aug 7, 2022

@jilleJr Wouldn't it make more sense to upstream data-ema-reload (as data-morphdom-config for instance) to https://github.com/patrick-steele-idem/morphdom rather than implement it in Ema?

@srid srid added this to the 1.0 milestone Aug 7, 2022
@applejag
Copy link

applejag commented Aug 7, 2022

@jilleJr Wouldn't it make more sense to upstream data-ema-reload (as data-morphdom-config for instance) to https://github.com/patrick-steele-idem/morphdom rather than implement it in Ema?

Yes, well kind of. The script reloading is done by Ema, and it'd be great if that was configurable as well on a per-script-node basis.

@sophicshift
Copy link
Collaborator

sophicshift commented Sep 17, 2022

I'm facing ema shim-related difficulties again. Some functions in JS must be called only once after the page is fully loaded, and sadly it's hard to do this currently. The DOMContentLoaded does not get called after route switches, and the EmaHotReload gets called many times. Another problem is properly sanitizing the environment after leaving the page, because event listeners that were added to the window will remain there.1

Honestly, I don't know how to work around this issue, and I feel it would be much simpler if ema did not try to replicate all the route switching work and instead leave it to the browser, which does that very well. Instead, it would only live reload inside the same route.

But as you pointed out in the other issue, people may prefer the current behavior, so the shim could be customizable. Would you accept a PR adding the possibility for using a different ema shim (from the haskell side)?

Footnotes

  1. (more detailed explanation, irrelevant to my suggestion)
    Let's say I have script X that I only want to get loaded on specific pages (katex, mathjax or reveal.js for instance).
    In general, I can make things work in a single page with live-reload, that is, when the user is not changing between pages but may be editing the current page. In this scenario, there is one event (DOMContentLoaded) that gets called only once, setting up things like listeners to the EMABeforeMorphDOMand EMAHotReload events.
    But if the user comes from a different page, this DOMContentLoaded event is not fired by ema, so things are not properly loaded.
    And when leaving to another page, the event listeners from the previous pages are still there, and are not removed.

@srid
Copy link
Owner Author

srid commented Oct 10, 2022

Sorry, I let this comment slip by.

@lucasvreis Would you accept a PR adding the possibility for using a different ema shim (from the haskell side)?

Can you elaborate on this? I'm generally happy to see shim improvements.

@srid
Copy link
Owner Author

srid commented Oct 10, 2022

More generally, the WebSocket support should probably be decoupled from ema in some fashion so that users can plug in their own stuff if they want.

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

No branches or pull requests

3 participants