shadow-cljs

https://github.com/thheller/shadow-cljs | https://github.com/sponsors/thheller | https://www.patreon.com/thheller
tomrbowden 2021-03-02T10:13:06.194500Z

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?

thheller 2021-03-02T10:14:34.194800Z

don't eval infinite sequences? 😛

thheller 2021-03-02T10:14:49.195100Z

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

tomrbowden 2021-03-02T10:29:02.195500Z

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

tomrbowden 2021-03-02T10:31:42.195800Z

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…

tomrbowden 2021-03-02T10:33:40.196Z

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

thheller 2021-03-02T10:33:57.196200Z

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

thheller 2021-03-02T10:34:04.196400Z

so that is the problem you need to solve

thheller 2021-03-02T10:34:29.196600Z

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

thheller 2021-03-02T10:34:48.196800Z

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

tomrbowden 2021-03-02T10:37:05.197Z

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

thheller 2021-03-02T10:37:36.197200Z

(str @evaluated-output)

tomrbowden 2021-03-02T10:37:48.197400Z

Yes, I see what you’re saying now

thheller 2021-03-02T10:37:50.197600Z

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

thheller 2021-03-02T10:38:20.197800Z

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

tomrbowden 2021-03-02T10:38:27.198Z

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

thheller 2021-03-02T10:38:44.198200Z

pr-str produces EDN output

thheller 2021-03-02T10:38:49.198400Z

str calls toString (which will not always be EDN)

👍 1
tomrbowden 2021-03-02T10:40:48.198800Z

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

yuhan 2021-03-02T11:26:56.199500Z

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

thheller 2021-03-02T11:28:27.199700Z

no

yuhan 2021-03-02T11:39:45.200800Z

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