Fork me on GitHub
#figwheel
<
2018-08-04
>
llsouder12:08:33

I have a three.js project. Everytime I save my files, figwheel re-runs my setup function which I put in a (defonce onceplease (setup!)).

llsouder12:08:50

I see a println and the other evidence this setup is being run everytime I save my files. What is the correct way to make a function only run once?

llsouder13:08:42

I changed to

(defonce setup-stuff  (do (setup)(init))) 
searched for other calls to setup and still I have things setting up on every reload.

bhauman18:08:08

@mikerod thats an interesting problem, first I'd say see if this works with the cljs.build.api/build call, because I'd be surprised if this is a figwheel problem in that it just calls the cljs.build.api

bhauman18:08:12

second you can work around this by resolving a namespaced symbol

bhauman18:08:57

@llsouder if setup or init return nil defonce will not work because setup-stuff has to have a value to detect that it has been defined

mikerod18:08:08

Thanks @bc8ze yeah I asked it more generally on cljs channel and got some clarity on the situation. David Nolen had a good point that the resolve would be in the ns the macro expands in. However it can be tricky because clojure.core vars would also not resolve. But he also pointed out you can use the cljs analyzer resolve instead for cljs macros. It was demonstrated in cljs spec alpha code. So anyways. I think figwheel isn’t doing anything odd here. It’s just cljs stuff

llsouder19:08:13

@bhauman thanks. changing the code to

(defonce setup-stuff  
  (do 
    (setup)
    (init)
    :done))  
fix it!