reagent

prabhanshu 2024-01-20T18:01:14.798549Z

I'm toying with some reagent code to understand how ratoms work internally. Here I assumed this component would re-render indefinitely – but it only renders once. Would appreciate any pointers on how reagent prevents this from looping.

(def an-atom (r/atom nil))

(defn a-component []
  (fn [] 
    @an-atom
    (reset! an-atom (rand-int 1000))))

(defn main []
  [a-component])

p-himik 2024-01-20T18:03:02.386079Z

I would suggest to straight up read the implementation. Much more effective than trying to approach it as a black box. It's not that easy to follow, but possible.

p-himik 2024-01-20T18:04:18.923529Z

But if I had to guess, it's probably because your component doesn't return proper Hiccup. Try wrapping @an-atom in a [:span ...] and return it from the inner function.

👀 1
prabhanshu 2024-01-20T18:09:06.889339Z

I tried this, doesn't loop either.

(defn a-component []
  (fn [] 
    [:div 
     [:span (reset! an-atom (rand-int 100))]
     [:span @an-atom]]))
I'm working through the source slowly but steadily, will report if I figure it out 😄

Charles Comstock 2024-01-20T21:20:03.967399Z

Why is there a second wrapping function call inside a-component? I think it's not actually evaluating the function but just trying to render a function as a value. Also, I suspect you want the reset to be for side-effects but not rendered in hiccup as I believe it just returns nil?

p-himik 2024-01-20T21:20:53.131389Z

> Why is there a second wrapping function call inside a-component? It's a form-2 component, perfectly fine. > I believe it just returns nil? Doesn't matter in this case.

Charles Comstock 2024-01-20T21:22:43.027239Z

I understand it's a two form component, but why, it's not closing over the state of an-atom so it's just complicating things for not a lot of reason.

Charles Comstock 2024-01-20T21:23:26.340309Z

like if an-atom was declared as a let around the fn I could see why it needs it but otherwise it's just kinda obscuring intent and complicating matters.

p-himik 2024-01-20T21:23:55.920979Z

> but why The OP is just experimenting, the question is not about good practices. :)

Charles Comstock 2024-01-20T21:24:35.130819Z

Sure, but if they are confused then it's helpful to simplify the setup until they understand it.

prabhanshu 2024-01-21T05:28:12.479969Z

> Why is there a second wrapping function call inside a-component? Oops yeah, it doesn't need to be form-2. I was playing around with a let and then missed removing the fn. A form-1 component illustrates it just fine.

escherize 2024-01-20T01:30:39.896269Z

Did anyone here watch the London Clojurians talk about HumbleUI?? The approach is so similar to reagent I think it will feel very natural to be able to just start writing desktop apps. ✍️ 💻

escherize 2024-01-20T03:01:38.872729Z

Talk here: https://www.youtube.com/watch?v=HZTrfz-2yW4

p-himik 2024-01-20T05:01:05.442499Z

I skimmed though the transcript and it seems that that approach is not too dissimilar from the already existing https://github.com/cljfx/cljfx And to be fair, Hiccup itself is not that different in most aspects from just calling the things (and Hiccup also introduces new problems), which is exactly what https://github.com/clj-commons/seesaw does. Of course, not bashing the work that Nikita has been doing. But IMO it's worth pointing out that people that want to write desktop UIs in Clojure without CLJS have had working solutions for quite some time now.

andre 2024-02-14T10:17:58.006999Z

you could always do this https://www.reddit.com/r/Clojure/comments/xof0id/tauri_clojurescript_shadowcljs/

p-himik 2024-02-14T10:25:16.090249Z

But that uses CLJS, quite a different approach.

andre 2024-02-14T10:54:42.947559Z

yes much better

p-himik 2024-02-14T11:00:11.041969Z

You mean CLJS is better? That's... debatable at the very least.