Fork me on GitHub
#shadow-cljs
<
2021-03-02
>
tomrbowden10:03:06

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?

thheller10:03:34

don't eval infinite sequences? 😛

thheller10:03:49

I'm guessing you call pr-str or so on the result?

tomrbowden10:03:02

I use cljs/eval-str and show the result in a div

tomrbowden10:03:42

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…

tomrbowden10:03:40

Is there a way to force-kill the infinite evaluation? (like hitting Ctrl-C when hitting an infinite loop in a command-line REPL?)

thheller10:03:57

well HOW do you display it in a div? I assume pr-str? THAT is were your infinite sequence is realized

thheller10:03:04

so that is the problem you need to solve

thheller10:03:29

eg. you can set *print-length* to limit the amount of items printed

thheller10:03:48

not exactly sure how you do that in self-hosted but something to look into

tomrbowden10:03:05

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)]]

thheller10:03:36

(str @evaluated-output)

tomrbowden10:03:48

Yes, I see what you’re saying now

thheller10:03:50

should be using pr-str in either case but str is the same in this case

thheller10:03:20

(binding [*print-length* 5] (pr-str @evaluated-output)) might be enough

tomrbowden10:03:27

I’m not clear what the difference it, but I will put pr-str then

thheller10:03:44

pr-str produces EDN output

thheller10:03:49

str calls toString (which will not always be EDN)

👍 3
tomrbowden10:03:48

That works perfectly! You’re awesome, @U05224H0W Thanks so much!!!

yuhan11:03:56

Is there any way to get shadow-cljs to watch and hot-reload changes to index.html?

yuhan11:03:45

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