I’m experimenting with self-hosted cljs in the browser, bootstrapped with shadow-cljs. I’m evaluating cljs typed into a textarea, eval’ed with the on-change event (debounced by a few hundred milliseconds). It works fine mostly, however, I’ve noticed that when typing in forms/expressions that yield infinite lazy sequences, the browser seizes up and stops responding. For example, typing (range)
causes this behavior. Are there any good strategies for dealing with this?
don't eval infinite sequences? 😛
I'm guessing you call pr-str
or so on the result?
I use cljs/eval-str
and show the result in a div
The problem I have is that I can’t prevent users of the web-app from evaluating infinite series that they’ve input into the textarea…
Is there a way to force-kill the infinite evaluation? (like hitting Ctrl-C when hitting an infinite loop in a command-line REPL?)
well HOW do you display it in a div? I assume pr-str
? THAT is were your infinite sequence is realized
so that is the problem you need to solve
eg. you can set *print-length*
to limit the amount of items printed
not exactly sure how you do that in self-hosted but something to look into
No, I simply do this:
(defonce evaluated-output (r/atom nil))
(defn compile-it [code]
(let [options {:eval cljs/js-eval
:load (partial boot/load c-state)}
callback (fn [result]
(reset! evaluated-output (:value result)))]
(cljs/eval-str c-state code "[demo-bootstrap-cljs]" options callback)))
and then I render it:
[:pre>code "Output: " [:strong (str @evaluated-output)]]
(str @evaluated-output)
Yes, I see what you’re saying now
should be using pr-str
in either case but str
is the same in this case
(binding [*print-length* 5] (pr-str @evaluated-output))
might be enough
I’m not clear what the difference it, but I will put pr-str
then
pr-str
produces EDN output
str
calls toString
(which will not always be EDN)
That works perfectly! You’re awesome, @thheller Thanks so much!!!
Is there any way to get shadow-cljs to watch and hot-reload changes to index.html
?
no
Thanks! Some note of this in the documentation would be great - it's not so clear what "monitor your source files and recompile them automatically" encompasses exactly