sci 2025-01-24

Apologies, my JS is not very strong. I’m trying to write a webapp using scittle. I’d like to dynamically load some CLJS, so I have

the console says “foo not defined”, but if I wait a second and type it in again, foo is defined. I believe what’s happening is the second script is running before the CLJS has finished evaluating. Is there any way to stop that race?

Oh, duh, changing the second script to x-scittle makes the problem go away, but the more general problem of js calling cljs still exists

<script> is synchronous by default, so the above should work in the order like you'd expect it to be

of course you still need to make the function foo accessible to JS

right, but what tripped me up is is not , because scittle uses its own XMLHttpRequest

oh that makes sense

there is a way to manually evaluate the script tags, perhaps that works for you, let me check the API

eval-script-tags* could work, but it’s not exported

and suboptimally, requires creating a new DOM node to use

the way to do this is this: call this function:

scittle.core.disable_auto_eval()
first, to disable what it says. And then: (scittle.core.eval_script_tags ...) on the dots you can place the result of a querySelector, or if you don't provide anything, it'll just evaluate all the application/x-scittle ones. Unfortunately currently it doesn't return a promise such that you could chain your JS evaluation, but that could be fixed

Another way to work with this is perhaps first define your JavaScript and then call a callback from your CLJS scittle code

staying only in scittle for now is an option

Thanks! sci and scittle are both excellent

❤️ 1

<script type="application/javascript"
   var data = ...;
   window.doit = () => window.foo(data);
</script>
<script type="application/x-scittle">
(defn foo [data] ...)
(set! (.-foo js/window) foo)
(js/window.doit)
</script>