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!)).
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?
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.@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
second you can work around this by resolving a namespaced symbol
@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
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
@bhauman thanks. changing the code to
(defonce setup-stuff
(do
(setup)
(init)
:done))
fix it!