@tolitius: I converted all my server-side code to mount and it works well. I'm quite happy with the results!
But I also have a question. I would like to modify the state from elsewhere (other functions, not just lifecycle functions). Is that… legal? :simple_smile:
An example is: start defines a pub/sub structure, and later on I start subscribing channels to it. I need to keep track of subscribed channels so that on stop I can unsubscribe them properly.
Or should I just keep those in a separate atom?
you can have a ns to track the subscriptions, e.g. subscriptions/subscribe!
Then again, perhaps I don't have to do this at all… core.async
docs aren't that clear on this, but if you close the source of the pub, pub should close as well, which should also close all subscribed channels.
I guess I could also keep that additional state in an atom inside the defstate state
Well, for ClojureScript the picture isn't so rosy. In one of my namespaces, modifying the file causes the start function to be called twice. Not good.
Also, the order seems wrong: ns1 :require
's ns2, but ns1's start is called first, then ns2's start. Twice. I think I'll stick to manual state management here for now…
Could mount be somehow confused because it is only required in two namespaces?
What's worse, this problem seems to appear and disappear without any regularity that I can notice.
does this happen during the development?
Yes, with figwheel. But I checked all combinations of cleaning/rebuilding.
It seems to have goten rare (or nonexisting) once I converted the entire app to mount, but I'm still looking carefully.
Ok, so the problem definitely exists. I have a dispatcher
namespace, that is required by pretty much everything else. Changing that file results in start being called twice, also after browser reload. I have to clean JavaScript and restart the whole figwheel environment for things to work again. What's curious, it is only this namespace that seems to be so affected.
@jrychter: dispatcher
is on cljs
side, right?
also if you are not using it, can you try 0.1.9-SNAPSHOT
I find lein cljsbuild
to behave oddly at times (environmental), hence I am using boot for most of the projects, it always starts clean. I know it would be a lot to change, but I definitely recommend giving it a try.
> "the order seems wrong: ns1 :require
's ns2, but ns1's start is called first, then ns2's start"
mount simply obeys the compiler.
it could theoretically happen when moving states from one place to another.. without reloading
So, in order: everything I described in on cljs
side. Clojure side works great. I am not using 0.1.9-SNAPSHOT, will try right away.
As for boot, been there, done that — we tried boot in a project in our company, it has its own set of problems. Nothing is perfect.
What is curious is that after changing and saving dispatcher.cljs
, stop gets called (once) and start gets called (once). So everything looks good. But if I reload the app, start will get called twice. It will get called twice from then on, until I clean the JavaScript files.
Trying 0.1.9-SNAPSHOT
now.
I get the same thing with 0.1.9-SNAPSHOT
. I guess I must not change the dispatcher file, then. I will implement a one-shot atom checking if start was run twice.
I wonder whether mount detects that something with a state was reloaded before it was stopped and restarts it, and then you call (mount/start)
in the app which result in two start calls.
do you see messages about ns recompile in the console:
Yes. And everything works as expected, except for the dispatcher namespace. Triggering recompilation for that one does everything as expected (it stops and then starts everything that depends on it). But after a reload the start method gets called twice. Every time I start the app.
My current fix is to rm -rf resources/public/static/js
after this happens. This triggers a full recompilation next time I change something, and fixes the double-start problem.
But after a reload
: you mean after figwheel pushes changes to the browser you see :start
being called twice?
i.e. or when you reload the page in the browser..
After figwheel pushes changes to the browser things are fine. But after a browser reload (Command-R in my case) start gets called twice.
need to think more about it... would you mind to create an issue? when I have time I'd like to check whether it is caused be environment or this is truly a defect
Of course. I am still trying to narrow it down.
great, thanks!
What I find worrying is something I noticed right now:
The little number (2) on the left. I added a logging message at the toplevel of my dispatcher
namespace, and it also gets a (2). Why would things happen twice? Checked this both with figwheel and with a production cljsbuild with :advanced
optimization. Same result.
> with a production cljsbuild with :advanced
optimization
first time you open a page everything loads normally, but after you reload the page :start
gets called twice?
No, the last message was only about duplicated log message that I added in my namespace. I expected the namespace to be loaded just once, and to see one log message. Instead, I see two. mount doesn't seem to produce any logging when compiled with :advanced
.