Dynamic documents and lost updates
Hi, I was trying out the Brr backend and had a couple of questions.
let ui =
let count = Lwd.var 0 in
let on_click () = Lwd.set count (Lwd.peek count + 1) in
let button =
Lwd.map
~f:(fun i ->
let btn = El.button [El.txt' (string_of_int i)] in
Ev.listen Ev.click (fun _e -> on_click ()) (El.as_target btn);
btn)
(Lwd.get count)
in
Elwd.div [`R button]
The rest of the file is the same as in the Brr example. If I replace the last line with button, the button no longer updates.
- I'm guessing this is due to the mismatch between the pure interface and the statefulness of the DOM. Should we always ensure that the top-level node is not dynamic, or is there a better workaround? Maybe this could be documented somewhere.
- Is this what the commented line is meant to address?
Anyway, I really like Lwd so far, awesome work!
Hi Darius, sorry for the late answer.
Indeed, there is a mismatch, usually just at the node at the junction between Lwd and DOM.
The commented line is meant to address that though this solution has shortcomings too.
First it would be nice to check that the ui node is not already the only child of body. Otherwise the side effect might reset some internal browser state (such as focused element and selected text).
And then, it works if there is only a single child, since it removes all other nodes. That could be worked around, but I have no time to spend on that in the near future :/.
And thanks, I am glad that you like Lwd!
I am really busy for now but I would like to give it some love in the next two years.