adding ":started?" to avoid double starts

This commit is contained in:
anatoly 2015-10-20 10:26:35 -04:00
parent a63c725dcb
commit e9fc2e239c
3 changed files with 15 additions and 12 deletions

View file

@ -101,7 +101,7 @@ this `app-config`, being top level, can be used in other namespaces, including t
(defstate conn :start (create-connection app-config)) (defstate conn :start (create-connection app-config))
``` ```
[here](https://github.com/tolitius/mount/blob/master/test/mount/nyse.clj) [here](https://github.com/tolitius/mount/blob/master/test/app/nyse.clj)
is an example of a Datomic connection that "depends" on a similar `app-config`. is an example of a Datomic connection that "depends" on a similar `app-config`.
## The Importance of Being Reloadable ## The Importance of Being Reloadable
@ -115,7 +115,7 @@ an example.
## Mount and Develop! ## Mount and Develop!
`mount` comes with an example [app](https://github.com/tolitius/mount/blob/master/test/mount/app.clj) `mount` comes with an example [app](https://github.com/tolitius/mount/tree/master/test/app)
that has two states: that has two states:
* `config`, loaded from the files and refreshed on each `(reset)` * `config`, loaded from the files and refreshed on each `(reset)`
@ -160,9 +160,9 @@ dev=> (reset)
notice that it stopped and started again. notice that it stopped and started again.
In nyse's connection [:stop](https://github.com/tolitius/mount/blob/a7424d4895ad7abe4933b425e718a6bdf1a0c22f/test/mount/nyse.clj#L18) In nyse's connection [:stop](https://github.com/tolitius/mount/blob/master/test/app/nyse.clj#L18)
function database is deleted. Hence after `(reset)` was called the app was brought its starting point: database was created by the function database is deleted. Hence after `(reset)` was called the app was brought its starting point: database was created by the
[:start](https://github.com/tolitius/mount/blob/a7424d4895ad7abe4933b425e718a6bdf1a0c22f/test/mount/nyse.clj#L11) function, [:start](https://github.com/tolitius/mount/blob/master/test/app/nyse.clj#L11) function,
but no schema again: but no schema again:
```clojure ```clojure

View file

@ -25,7 +25,7 @@
(defn go (defn go
"Initializes and starts the system running." "Initializes and starts the system running."
[] []
;; (refresh) would redefine "defstates" which will call "start" on them (start)
:ready) :ready)
(defn reset (defn reset

View file

@ -11,24 +11,27 @@
(defmacro defstate [state & body] (defmacro defstate [state & body]
(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 (-> {:state (str `~state) :start `(fn [] (~@start))} (let [s-meta (-> {:state (str `~state) :start `(fn [] (~@start)) :started? true}
(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))))) (~@start)))))
(defn- up [{:keys [ns name start]}] (defn- up [var {:keys [ns name start started?]}]
(intern ns (symbol name) (start))) (when-not started?
(intern ns (symbol name) (start))
(alter-meta! var assoc :started? true)))
(defn- down [{:keys [stop]}] (defn- down [var {:keys [stop started?]}]
(when stop (when started?
(stop))) (alter-meta! var assoc :started? false)
(when stop (stop))))
(defn- f-states [f] (defn- f-states [f]
(->> (all-ns) (->> (all-ns)
(mapcat ns-interns) (mapcat ns-interns)
(map second) (map second)
(filter #(:state (meta %))) (filter #(:state (meta %)))
(map (comp f meta)))) (map #(f % (meta %)))))
(defn start [] (defn start []
(doall (doall