defstate as NotStartedState at first

This commit is contained in:
anatoly 2015-10-25 21:41:12 -04:00
parent 82472e3b7b
commit bba76ff0a5

View file

@ -15,6 +15,11 @@
(swap! state-order assoc state nseq) (swap! state-order assoc state nseq)
nseq))) nseq)))
(deftype NotStartedState [state]
Object
(toString [this]
(str "'" state "' is not started (to start all the states call mount/start)")))
;;TODO validate stop and the fact that start and stop are fns ;;TODO validate stop and the fact that start and stop are fns
(defn- validate [{:keys [start stop]}] (defn- validate [{:keys [start stop]}]
(when-not start (when-not start
@ -22,20 +27,19 @@
{:start start :stop stop}) {:start start :stop stop})
(defmacro defstate [state & body] (defmacro defstate [state & body]
(debug ">> starting.. " state)
(let [[state [c cf d df]] (macro/name-with-attributes state body) (let [[state [c cf d df]] (macro/name-with-attributes state body)
{:keys [start stop]} (validate {c cf d df})] {:keys [start stop]} (validate {c cf d df})]
(let [s-meta (-> {:session-id session-id (let [s-meta (-> {:session-id session-id
:order (make-state-seq state) :order (make-state-seq state)
:start `(fn [] (~@start)) :start `(fn [] (~@start))
:started? true} :started? false}
(cond-> df (assoc :stop `(fn [] (~@stop)))))] (cond-> df (assoc :stop `(fn [] (~@stop)))))]
`(defonce ~(with-meta state (merge (meta state) s-meta)) `(defonce ~(with-meta state (merge (meta state) s-meta))
(~@start))))) (NotStartedState. ~(str state))))))
(defn- up [var {:keys [ns name start started?]}] (defn- up [var {:keys [ns name start started?]}]
(when-not started? (when-not started?
(debug ">> starting.. " name) (info ">> starting.. " name)
(let [s (try (start) (let [s (try (start)
(catch Throwable t (catch Throwable t
(throw (RuntimeException. (str "could not start [" name "] due to") t))))] (throw (RuntimeException. (str "could not start [" name "] due to") t))))]
@ -44,7 +48,7 @@
(defn- down [var {:keys [name stop started?]}] (defn- down [var {:keys [name stop started?]}]
(when started? (when started?
(debug "<< stopping.. " name) (info "<< stopping.. " name)
(when stop (when stop
(try (try
(stop) (stop)