#22, #25, #26: states with no :stop are restarted on ns recompile

This commit is contained in:
anatoly 2015-12-23 11:04:47 -05:00
parent c19ef8af9a
commit f98df5b2c4
3 changed files with 19 additions and 6 deletions

View file

@ -421,13 +421,16 @@ Switched to branch 'suspendable'
Mount will detect when a namespace with states (i.e. with `(defstate ...)`) was reloaded/recompiled,
and will check every state in this namespace whether it was running at the point of recompilation. If it was, _it will restart it_:
* will invoke its `:stop` function that was there _before_ the recompilation
* will invoke its new `:start` function _after_ this state is recompiled/redefined
* if a state has a `:stop` function, mount will invoke it on the old version of state (i.e. cleanup)
* it will call a "new" `:start` function _after_ this state is recompiled/redefined
Mount won't keep it a secret, it'll tell you about all the states that had to be restarted during namespace reload/recompilation:
<img src="doc/img/ns-recompile.png" width="500px">
Providing a `:stop` function _is_ optional, but in case a state needs to be cleaned between restarts or on a system shutdown,
`:stop` is highly recommended.
## Affected States
Every time a lifecycle function (start/stop/suspend/resume) is called mount will return all the states that were affected:

View file

@ -48,9 +48,10 @@
this function stops this 'lost' state instance.
it is meant to be called by defstate before defining a new state"
[state]
(when-let [stop (@running state)]
(prn (str "<< stopping.. " state " (namespace was recompiled)"))
(stop)
(when-let [{:keys [stop] :as up} (@running state)]
(when stop
(prn (str "<< stopping.. " state " (namespace was recompiled)"))
(stop))
(swap! running dissoc state)
{:restart? true}))
@ -90,7 +91,7 @@
(record! state resume done)
(record! state start done)))]
(alter-state! current s)
(swap! running assoc state stop)
(swap! running assoc state {:stop stop})
(update-meta! [state :status] #{:started}))))
(defn- down [state {:keys [stop status] :as current} done]

View file

@ -31,6 +31,15 @@
(mount/stop)
(is (instance? mount.core.NotStartedState (dval tapp.example/nrepl))))))
#?(:clj
(deftest start-on-recompile
(let [_ (mount/start)
before (dval tapp.conf/config)]
(require 'tapp.conf :reload)
(is (not (identical? before (dval tapp.conf/config)))) ;; should be a newly recompiled map
(mount/stop)
(is (instance? mount.core.NotStartedState (dval tapp.conf/config))))))
#?(:cljs
(deftest cleanup-dirty-states
(let [_ (mount/start #'mount.test.helper/helper)]