Is there something that behaves like agent in clojurescript ?
I'd like to port my clojure library to cljs but it uses agent because it needs their asynchronicity
Or I guess I can use reader conditionals to use an atom when using cljs and an agent when using clojure
According to the https://clojurescript.org/about/differences#_concurrent_programming “Agents are currently not implemented”.
javascript is single-threaded so agent would just act like atoms
I know, hence my question 😛
Makes sense, thank you
not exactly, e.g for nested updates the ordering of operations in not the same
fair engough 😅
Hello. I am trying to open a file dialog box. I tried the following code without any results.
(.. js/document (getElementById "file-upload") click)
When I tried running `document.getElementById("file-upload").click()`
the dialog opened. Could you suggest a fix?
there are limits in what the browser lets you do. for security reasons certain things are only allowed when actually triggered by a user event and so on. you can't just randomly open a dialog box like that, browser will block or just ignore the request. trying to do that in the browser console may bypass those restrictions.
Hey Guys, I'm kind of stuck with the same problem for weeks now. I'm pretty sure the solution is quite simple but I didn't get it on my own. So here's the problem: I have a text input field with a value. The value is within a nested atom so it's mutable, in theory 😉... With a simple atom it's working, but not with a nested one. At first I can't change the value in the input field and second if I choose for an input field without value I can change it but the output doesn't change. That's the code:
(defn inputPage [title rNames rSizes rCost url]
(let [room-property (r/atom {:title title
:name rNames
:Size rSizes
:Costs rCost
:img-scr url})]
(if-not (blank? rNames)
[:div {:class "inputLine"}
[:img {:src url :class "roomImg"}]
[:LineContainer
[:TextLine [:p {:class "headLine"} [:b rNames " " ] [:comment "(" (zeroDec (* rCost dd-costKey))" €/qm)"]]
[:p {:class "roomLine"} (:Size @room-property) " qm, " [:class {:class "result"} [:u "Summe: " (zeroDec (* rCost dd-costKey rSizes))" €"]]]]
]
[:span
[:b rNames " " ]
[:input {:type "text"
:name :Size
:value (:Size @room-property)
:on-change #(reset! [room-property :Size] (-> % .-target .-value))}]
[:p {:id "eOutputCell" } (:Size @room-property) ]]]
))
)
I solved it finally. The hints from thheller and lassemaata where very helpful. Thank you Guys. The solution was a fn [] around the component.
So this is it:
(defn atom-input [value]
[:input {:type "text"
:value (:Size @value)
:on-change #(swap! value assoc :Size (.. % -target -value))}]
)
(defn inputPage [title rNames rSizes rCost url]
(let [room-property (r/atom {:title title
:name rNames
:Size rSizes
:Costs rCost
:img-scr url})]
(if-not (blank? rNames)
(fn []
[:div {:class "inputLine"}
[:img {:src url :class "roomImg"}]
[:LineContainer
[:TextLine [:p {:class "headLine"} [:b rNames " " ] [:comment "(" (zeroDec (* rCost dd-costKey))" /qm)"]]
[:p {:class "roomLine"} (:Size @room-property) " qm, " [:class {:class "result"} [:u "Summe: " (zeroDec (* rCost dd-costKey rSizes))" "]]]]
]
[:span
[:b rNames ": " ]
[atom-input room-property]
[:p {:id "eOutputCell" } (:Size @room-property) ]]])
[:div {:class "inputLine"
:onClick #(testPage rSizes) }
[:plus]]
))
)
#(reset! [room-property :Size] (-> % .-target .-value))
is not valid. you are calling reset!
on the vector. you want to use swap!
and assoc
The interesting thing is that the on-click event handler is called, but the dialog is not displayed. My use case is the following: I am in tab A, when I click a button in A I have to navigate to B and click a button which opens the file dialog. Maybe a way to do it would be to navigate to B but open the dialog before navigation from A
Thanks I will try it. But what is the reason I can't change the input field? There must be anything totally wrong. 😞
yeah browser restrictions are weird in some cases. I can't remember the exact rules either but my guess would be that is the cause of your issue.
it keeps the current value because you :on-change
fails, see https://reactjs.org/docs/forms.html#controlled-components
Ok nice thanks for the explanation and the link. I'm not aware that there are dependencies between.
you can also do uncontrolled components https://reactjs.org/docs/uncontrolled-components.html
also, shouldn't that component return a function? otherwise the atom is re-initialized every time the arguments change? (disclaimer: I haven't done cljs in ages)
that too probably
thanks for your help
😂I have absolutely no idea what you guys are talking about. I'm an absolute beginner but I will work through the links and hopefully I will understand your remarks after that. Thank you so much
it might also be a good idea to check out the reagent documentation, e.g. https://github.com/reagent-project/reagent/blob/master/doc/CreatingReagentComponents.md
👍
Where in the CLJS tests can I find the unit tests for JS interop like (.- ...)
, (.. ...)
and (js/foo.bar ..)
etc?
oh boy do I feel dumb. I just learned the hard way that (prn ..)
in a macro emits the printed text into the compiled file, at least under Figwheel main dev builds.
it happened that the output in my case kinda resembled JS code, and I was getting syntax errors in the browser that made no sense at first.
i think *out*
is bound to the output file during compilation?
i think this might be it: https://github.com/clojure/clojurescript/blob/master/src/main/clojure/cljs/compiler.cljc#L1529-L1530
I'm executing this code from a cljs
file, after installing cystoscape
via npm
(I'm using shadow-cljs
(ns minimal.core
(:require
["cytoscape" :as cytoscape]))
(def graph
(cytoscape
(clj->js {:container (.getElementById js/document "graph")
:elements [{:group "nodes" :data {:id 1 :immediate "a"}}
{:group "nodes" :data {:id 2 :immediate "b"}}
{:group "edges" :data {:id 12 :source 1 :target 2 :immediate "meh"}}]
:style [{:selector "node"
:style {:label "data(immediate)"}}]})))
This works, and displays everything as wanted, but then I do:
(.add graph (clj->js {:group "nodes" :data {:id 3 :immediate "c"}}))
and it works, but in the browser console I also get:
Collection [Element(1), _private: {…}]0: Element [Element(1), _private: {…}]length: 1_private: {eles: Collection(1), cy: Core, rebuildMap: ƒ}__proto__: Array RangeError: Maximum call stack size exceeded
at Object.eval [as cljs$core$ILookup$_lookup$arity$2] (cljs.core.js:23845)
at Function.eval [as cljs$core$IFn$_invoke$arity$2] (cljs.core.js:6641)
at Object.eval [as cljs$core$IFn$_invoke$arity$1] (cljs.core.js:11321)
at cljs$core$pr_writer (cljs.core.js:33105)
at Object.cljs$core$pr_sequential_writer [as pr_sequential_writer] (cljs.core.js:32802)
at Object.cljs$core$pr_writer_impl [as pr_writer_impl] (cljs.core.js:33011)
at cljs$core$pr_writer (cljs.core.js:33113)
at Object.cljs$core$pr_sequential_writer [as pr_sequential_writer] (cljs.core.js:32802)
at Object.cljs$core$pr_writer_impl [as pr_writer_impl] (cljs.core.js:33011)
at cljs$core$pr_writer (cljs.core.js:33113)
I googled, for the error, and it seems related to clojurescript, but I can't figure why. Any pointers?If you try (do (your form above) nil) does it work?
it does, and doesn't cause the error! But why?
The error above had some stack traces in the printer. So it seemed like it was “working” but just blowing up printing the result in the repl
I see! Thanks a lot for explaining that!
and is writing (do MyForm nil)
(or defining (silence MyForm)
the best I can do here?
You can’t print the value. But the value itself is fine. If you’re using the repl the p is killing you. If it’s just in regular code or not the return value of an expression if printed it’s fine
I see, thanks again 🙂
You know, looking into eval in clojurescript some more, is there any reason why find-ns-obj
can't work in :target :bundle
?
@leif if you need sandboxed eval in CLJS, you might want to look at sci