clog icon indicating copy to clipboard operation
clog copied to clipboard

Synchronization of DOM changes from Lisp code and inline <script>s

Open khinsen opened this issue 6 months ago • 2 comments

When I replace the content of an element via clog:inner-html, any <script> elements with inline code in the supplied HTML are executed, but the detailed rules for this execution are not clear. A quick glance at the html() function in JQuery, which is what clog:inner-html ends up calling, only shows that the rules are likely to be complicated.

If such embedded scripts change the DOM, it is important to wait for the end of their execution before inspecting or manipulation the DOM from CLOG. I tried to do this by adding a <script> element at the end of the HTML code that I injected via clog:inner-html. This script does $(clog['document']).trigger('on-load-script','~A'), where ~A gets replaced by a unique token (a gensym). Then I add a handler for the on-load-script event, which uses a semaphore to signal the end of execution to the Lisp level, using the unique token to distinguish between possibly multiple events.

This mechanism frequently fails, in that the semaphore signal arrives before the scripts embedded in the HTML have terminated. As a consequence, my Lisp code working on the DOM sees some intermediate state.

khinsen avatar Aug 12 '24 19:08 khinsen