Fork me on GitHub
#clojurescript
<
2022-02-05
>
Richie00:02:37

Hey. I want to do some stuff before and after every function in my namespace. I learned that I could use set! but it doesn't work when I try to loop over multiple functions. It doesn't make a difference whether I use hello, 'hello, or #'hello . What's going on here? How can I understand this code? Reading https://github.com/clojure/clojurescript/blob/master/src/main/clojure/cljs/analyzer.cljc#L2570 didn't help.

(defn hello [] (println "hello world"))
(set! hello (fn [] (println "goodbye")))
(hello)
;; works fine

((fn [v]
   (set! v (fn [] (println "goodbye"))))
 hello)
;; fails
;; Can't set! local var or non-mutable field

Richie00:02:34

At first, I was afraid that I'm rehashing https://clojurians.slack.com/archives/C03S1L9DN/p1561734105283300. After re-reading the response though, now I'm thinking that I should be able to get what I want. I would need alter-var-root if I had vars.

oxalorg (Mitesh)11:02:59

Whats the use case here? Curious to know. Have you looked at 1. Creating a macro which wraps? 2. Simply wrapping all calls with another function at the caller location?

Richie15:02:17

I don't want to modify every function. Kind of like how I can spec functions without modifying the call site. https://clojurians.slack.com/archives/C03S1L9DN/p1643947600246099?thread_ts=1643943819.408319&amp;cid=C03S1L9DN looking at how spec works since I'm trying to do the same thing. That's good advice except I don't understand the code and have not been able to make it work for me.

oxalorg (Mitesh)06:02:16

> I don't want to modify every function. Are you looking for something where you turn defn my-func to defntrace my-func and thats it, or do you want to dynamically bind to every function without changin anything? For the former you can do this with a macro. The simplest looking macro can be this:

(defntrace defwrap [sym bindings & body]
  `(defn ~sym ~bindings
     (println "Calling this before callign your func")
     ;; start trace here
     ~@body))

Richie14:02:08

Thanks for the suggestion but I don't want to change all of my function definitions to defntrace. I'd like to not change anything.

Richie14:02:39

My plan is to log all the function calls in a format compatible with chrome's trace events viewer.

oxalorg (Mitesh)14:02:44

Yeah I am not sure how that would happen or if it is even possible for now. You'd have to probably hook into the cljs compilation step, possibly after it reads the forms but before compiles. :thinking_face:

Richie14:02:25

I looked into that but I didn't find any function objects in the build context. I only see my code in .shadow-cljs/ under .shadow-cljs/bulids/web/dev/ana/my-project/app.cljs.transit.json If I read the json, changed the source code, and wrote it back out, would that even give me what I want? That seems excessive. I only looked at the :compile-prepare stage.

(defn hook
  {:shadow.build/stage :compile-prepare}
  [ctx]
  (pprint ctx)
  ctx)

1
Muhammad Hamza Chippa01:02:17

I am trying to use the local image but it is not loading hiccups i have tried all of the three way of defining url but it is not working (image is in the root) [:img {:src "./pressure.png"}] [:img {:src "/pressure.png"}] [:img {:src "pressure.png"}]

Richie02:02:11

I'm using shadow-cljs and in my shadow-cljs.edn file I've configured :dev-http {8081 "public"} I put icon.png in the public folder and my hiccup looks like [:img {:src "icon.png"}] and it's working

✔️ 1
oxalorg (Mitesh)11:02:52

(image is in the root) root of the project? or root of the http server?

vaibhav chaturvedi16:02:57

Hi, I am trying to use Krell to create a cljs-react-native app. I need to use AppState, but it appears that reagent.react-native doesn't provide that. (krell docs use reagent.react-native) What is the proper way to use all methods of react-native in cljs-rn app (I guess using krell or not shouldn't matter here) ?

1
Michaël Salihi18:02:01

Hi @vaibhawc, you can import directly what you want.

(ns your.app.namspace
  (:require ["react-native" :as rn])
Check how reagent-react-native does it: https://github.com/vouch-opensource/reagent-react-native/blob/master/src/reagent/react_native.cljs

Michaël Salihi18:02:06

Then do some JS interop like (def app-state (r/atom (.-currentState rn/AppState))

✔️ 1
vaibhav chaturvedi19:02:28

That worked but without "". Thanks, this solves many other Qs I was having.

Michaël Salihi20:02:37

You're welcome @vaibhawc , have fun!

Michaël Salihi21:02:39

> That worked but without "". @vaibhawc FYI, it should works with double quote too...so it's strange. Just curious, what error did you get?

vaibhav chaturvedi16:02:14

Very weird. It was giving undefined for rn earlier. But when I restarted everything (krell-repl and metro), it has vanished. It works with "", though I prefer without "".

Luciano Laratelli21:02:44

Hello 🙂 I have a Clojure macro that reads file contents. I call it to render the contents in a Clojurescript frontend. This works well, but the feedback loop is slow when I make edits to the files. I tried opening a CLJ sibling REPL, re-evaluating the macro definition, and calling the macro on the Clojure side, but that didn’t work. Is there a way to re-read the files when I edit them without having to quit my REPL entirely? If it matters, I’m using CIDER and shadow-cljs.

Luciano Laratelli21:02:26

That’s gorgeous, thank you 🙂

Drew Verlee03:02:34

Out of curiosity, why does it need to be a macro?

oxalorg (Mitesh)06:02:05

@U0DJ4T5U1 If your cljs is targeting the browser there is no way for it to access the filesystem. But using a clojure macro which binds at compile time, so when the cljs->js compilation happens we have access to clojure which has access to filesystem 🙂

thheller07:02:47

for most cases you should probably still be loading the file dynamically at runtime though. the macros are only really meant for really small files.

2
👍 3