#22: restarting a state on ns recompile

vs. just stopping it, which is not well expected from the users
This commit is contained in:
anatoly 2015-12-22 23:01:11 -05:00
parent 4e5e4fba71
commit 2d25fd9b58
3 changed files with 20 additions and 6 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 43 KiB

View file

@ -51,7 +51,8 @@
(when-let [stop (@running state)] (when-let [stop (@running state)]
(prn (str "<< stopping.. " state " (namespace was recompiled)")) (prn (str "<< stopping.. " state " (namespace was recompiled)"))
(stop) (stop)
(swap! running dissoc state))) (swap! running dissoc state)
{:restart? true}))
#?(:clj #?(:clj
(defn current-state [state] (defn current-state [state]
@ -135,8 +136,8 @@
order (make-state-seq state-name) order (make-state-seq state-name)
sym (str state)] sym (str state)]
(validate lifecycle) (validate lifecycle)
(cleanup-if-dirty state-name) (let [{:keys [restart?]} (cleanup-if-dirty state-name)
(let [s-meta (cond-> {:order order s-meta (cond-> {:order order
:start `(fn [] ~start) :start `(fn [] ~start)
:status #{:stopped}} :status #{:stopped}}
stop (assoc :stop `(fn [] ~stop)) stop (assoc :stop `(fn [] ~stop))
@ -144,9 +145,13 @@
resume (assoc :resume `(fn [] ~resume)))] resume (assoc :resume `(fn [] ~resume)))]
`(do `(do
(def ~state (DerefableState. ~state-name)) (def ~state (DerefableState. ~state-name))
((var mount.core/update-meta!) [~state-name] (assoc ~s-meta :inst (atom (NotStartedState. ~state-name)) (let [meta# (assoc ~s-meta :inst (atom (NotStartedState. ~state-name))
:var (var ~state))) :var (var ~state))]
(var ~state)))))) ((var mount.core/update-meta!) [~state-name] meta#)
(when ~restart?
(prn (str ">> starting.. " ~state-name " (namespace was recompiled)"))
((var mount.core/up) ~state-name meta# (atom #{})))
(var ~state)))))))
#?(:clj #?(:clj
(defmacro defstate! [state & {:keys [start! stop!]}] (defmacro defstate! [state & {:keys [start! stop!]}]

View file

@ -22,6 +22,15 @@
(mount/stop) (mount/stop)
(is (instance? mount.core.NotStartedState (dval tapp.example/nrepl)))))) (is (instance? mount.core.NotStartedState (dval tapp.example/nrepl))))))
#?(:clj
(deftest restart-on-recompile
(let [_ (mount/start)
before (:server-socket (dval tapp.example/nrepl))]
(require 'tapp.example :reload)
(is (not= before (:server-socket (dval tapp.example/nrepl)))) ;; should have restarted on recompile/reload, hence different reference
(mount/stop)
(is (instance? mount.core.NotStartedState (dval tapp.example/nrepl))))))
#?(:cljs #?(:cljs
(deftest cleanup-dirty-states (deftest cleanup-dirty-states
(let [_ (mount/start #'mount.test.helper/helper)] (let [_ (mount/start #'mount.test.helper/helper)]