From e9fc2e239c877c9c9f217774ffffa1992e0149f5 Mon Sep 17 00:00:00 2001 From: anatoly Date: Tue, 20 Oct 2015 10:26:35 -0400 Subject: [PATCH] adding ":started?" to avoid double starts --- README.md | 8 ++++---- dev/dev.clj | 2 +- src/mount/mount.clj | 17 ++++++++++------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 48ff6c3..370e692 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dev/dev.clj b/dev/dev.clj index 3817f3c..17446c5 100644 --- a/dev/dev.clj +++ b/dev/dev.clj @@ -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 diff --git a/src/mount/mount.clj b/src/mount/mount.clj index ec909e8..f77f98d 100644 --- a/src/mount/mount.clj +++ b/src/mount/mount.clj @@ -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