-
Notifications
You must be signed in to change notification settings - Fork 73
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
Handling callbacks in Eio+Js #534
Conversation
Co-authored-by: Thomas Leonard <[email protected]>
Thanks @balat ! This is an important issue to address in any JS backend for Eio. This implementation looks sensible, I had originally raised concerns about the switch in the To test that I had a go at making the example a little more complicated in 2173aa7 which:
(Please don't take me changing jsoo to brr as hostile, I just know it a lot better!) |
Thank you. Yes of course it's much better if the main event loop is always running. About Brr: no problem at all. I just don't know how it works, that's why I used the standard js_of_ocaml library instead. We had in mind to build a version of the browser api without object types (using gen_js_ai) but I guess that's what Brr is. |
Can we use Eio promises instead of Fut? |
I do much prefer the non-object approach to writing OCaml in the browser. Really all we need is what amounts to Brr's Perhaps we could vendor our own version of this and just use that for doing any browser work we need to do in the internal
The way the code is structured right now is that we return a Javascript promise from the main function because we don't really know when the program is finished (someone might click on a button in ten minutes!). We can't use Eio promises at that point because we are outside the handler and awaiting would raise unhandled effect. I just realised that with the addition of |
BTW, sometimes browsers, especially mobile browsers, require certain actions to happen during the callback. I've run into this myself while developing a web app. I don't recall the exact details anymore, but it might have been something like e.g. making a certain input element active/focused or scrolling the page in response to a click or touch or some such event. So, what happened there was that my code performed the effect after the callback had returned and then the browser simply didn't perform it. So, while this kind of wrapping can be useful, it is not correct to simply wrap all callbacks. |
Superceded by #680. |
I'm opening this draft PR (not to be merged) to discuss how it is possible to use Eio in callbacks (JS event handlers ...).
It is built on top of @patricoferris ' PR #405 and adds just one commit with a basic solution to this problem.
The idea:
Drawbacks:
Feel free to comment and propose better ideas (global switch?).