janestreet / incr_dom

A library for building dynamic webapps, using Js_of_ocaml.
MIT License
376 stars 32 forks source link

Interactions between Incr_dom and Effects #18

Open Fitzgibbons opened 1 month ago

Fitzgibbons commented 1 month ago

Hi,

I recently tried to update an Incr_dom project with some code that uses effect handlers and ran into some issues. I've made a reproduction repo at incr_dom_effect_issue, and would appreciate advice on how to proceed.

While the example in the repo is reduced to something much simpler, the actual motivation for using effects in the first place is to synchronously wait on asynchronous javascript functions. I'm aware that this has drawbacks for the ui, but it seems reasonable in my case, as the asynchronous functions don't take long and are only called after button presses. If there's a more direct way to use asynchronous javascript functions from within Incr_dom I'd be happy to use it, but this feels like the "shape" of thing effects are good for, so I figured I'd ask the broader question.

Thanks, Fitz

TyOverby commented 1 month ago

Incr_dom hasn’t been tested with effect handlers yet, so I’m not surprised that there are issues. Could you describe the problem that you’re running into though?

TyOverby commented 1 month ago

Oh sorry, I see now that you said that the effect triggers an “unhandled” exception. Thats expected because Incr_dom.start just schedules stuff to happen on future frames and it returns immediately, so your handler is done

Fitzgibbons commented 1 month ago

Ah that makes sense. I don't have a good understanding of how Async ends up getting compiled to javascript, so it's possible I have a fundamental misunderstanding, but is there some way to effectively define my handler around the equivalent of Scheduler.go?

I'll also explore whether it's possible to move my effect handler into the apply_action function.

TyOverby commented 1 month ago

I think that incr_dom would need custom logic in the event loop for effects to work