adding ":started?" to avoid double starts
This commit is contained in:
parent
a63c725dcb
commit
e9fc2e239c
3 changed files with 15 additions and 12 deletions
|
|
@ -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))
|
||||
```
|
||||
|
||||
[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`.
|
||||
|
||||
## The Importance of Being Reloadable
|
||||
|
|
@ -115,7 +115,7 @@ an example.
|
|||
|
||||
## 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:
|
||||
|
||||
* `config`, loaded from the files and refreshed on each `(reset)`
|
||||
|
|
@ -160,9 +160,9 @@ dev=> (reset)
|
|||
|
||||
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
|
||||
[: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:
|
||||
|
||||
```clojure
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
(defn go
|
||||
"Initializes and starts the system running."
|
||||
[]
|
||||
;; (refresh) would redefine "defstates" which will call "start" on them
|
||||
(start)
|
||||
:ready)
|
||||
|
||||
(defn reset
|
||||
|
|
|
|||
|
|
@ -11,24 +11,27 @@
|
|||
(defmacro defstate [state & body]
|
||||
(let [[state [c cf d df]] (macro/name-with-attributes state body)
|
||||
{: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)))))]
|
||||
`(defonce ~(with-meta state (merge (meta state) s-meta))
|
||||
(~@start)))))
|
||||
|
||||
(defn- up [{:keys [ns name start]}]
|
||||
(intern ns (symbol name) (start)))
|
||||
(defn- up [var {:keys [ns name start started?]}]
|
||||
(when-not started?
|
||||
(intern ns (symbol name) (start))
|
||||
(alter-meta! var assoc :started? true)))
|
||||
|
||||
(defn- down [{:keys [stop]}]
|
||||
(when stop
|
||||
(stop)))
|
||||
(defn- down [var {:keys [stop started?]}]
|
||||
(when started?
|
||||
(alter-meta! var assoc :started? false)
|
||||
(when stop (stop))))
|
||||
|
||||
(defn- f-states [f]
|
||||
(->> (all-ns)
|
||||
(mapcat ns-interns)
|
||||
(map second)
|
||||
(filter #(:state (meta %)))
|
||||
(map (comp f meta))))
|
||||
(map #(f % (meta %)))))
|
||||
|
||||
(defn start []
|
||||
(doall
|
||||
|
|
|
|||
Loading…
Reference in a new issue