figwheel

general discussion about figwheel, specific discussions in #figwheel-main and #lein-figwheel
llsouder 2018-08-04T12:29:33.000034Z

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!)).

llsouder 2018-08-04T12:30:50.000008Z

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?

llsouder 2018-08-04T13:13:42.000054Z

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.

bhauman 2018-08-04T18:02:08.000002Z

@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

bhauman 2018-08-04T18:03:12.000059Z

second you can work around this by resolving a namespaced symbol

bhauman 2018-08-04T18:04:57.000070Z

@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

2018-08-04T18:17:08.000078Z

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

llsouder 2018-08-04T19:09:13.000059Z

@bhauman thanks. changing the code to

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