Fork me on GitHub
#clojurescript
<
2023-07-28
>
Reut Sharabani18:07:57

how can I know if a function is defined in cljs? I want to define it only if it's not defined. I tried:

(try
  (js/clickNode "test")
  (.log js/console "clicknode exists")
  (catch :default _e
    (.log js/console "defining callback")
    (set! js/clickNode (fn [node-id]
    (.log js/console "clicked node" node-id)))))
But I'm still seeing an (uncaught?) ReferenceError in the browser console and clickNode is not defined.

p-himik18:07:11

If you can help it, it's better not to change global things. If you can't help if, you can set a global with (set! js/window -clickNode (fn ...)).

👀 1
thheller19:07:32

you can also check (exists? js/clickNode) no need to call and catch

Reut Sharabani19:07:53

Thanks, both seem to work but they didn't fix my problem 😞 Not expecting an answer but for the curious: I'm trying something kind of niche - use mermaidjs node click events in clojurescript (specifically through hyperfiddle). It's not wotking. I thought it's related to the callback not being defined "in time" but it seems like it's a different problem. I also tried defn ^:export clickNode ... but that also didn't work. I have a java promise that returns an object with keys svg and bindFunctions. I need to call bindFunctions to make the svg clicks operate properly (probably to bind the functions to the svg elements). SVG is mounted and looks find but the bindfunctions thing is not working:

(when (exists? (.-clickNode js/window))
  (println "defining clickNode")
  (set! js/window -clickNode (fn [node-id]
                              (println "clicked node" node-id))))
(.then
  (.render js/mermaid "dummy" mmd)
  (fn [res]
    ;; (println mmd)
    (let [e (.getElementById js/document "mermaid")
          svg (.-svg res)
          bind-functions (.-bindFunctions res)]
      (set! (.-innerHTML e) svg)
      (when bind-functions
        (println "binding functions" bind-functions)
        (bind-functions e)))))
                          nil))))))
It could also be a problem of the library I'm using (hyperfiddle)

p-himik19:07:53

Where exactly do you use the clickNode function after it's been set?

Reut Sharabani19:07:05

in the graph definition, it's a line that says something like: click A clickNode And it means that when the node on svg is clicked it should invoke clickNode

Reut Sharabani19:07:23

To my understanding that's why you get a callback to bind functions after the svg has been generated

Reut Sharabani19:07:21

this is the library if you're interested: https://mermaid.js.org/config/usage.html

p-himik19:07:53

Those docs don't really say anything about the callbacks, or I didn't notice it. The "Binding events" section is as bare-bones as it gets and also has an incorrect explanation at the bottom.

Reut Sharabani19:07:20

yeah I had to dig into the code to understand some things. I'll debug it and I hope I remember to update here if it works. It's probably something in my code.

Reut Sharabani19:07:43

I'm also not usually using clojurescript or javascript so it's challenging.

Reut Sharabani11:07:34

updating that I decided to move to cytoscape for now