adding :session-id and :order markers
:order, once implemented, will allow to maintain stop/start order
This commit is contained in:
parent
ccd352dfa9
commit
8b0744cd63
1 changed files with 19 additions and 2 deletions
|
|
@ -1,7 +1,20 @@
|
||||||
(ns mount
|
(ns mount
|
||||||
(:require [clojure.tools.macro :as macro]
|
(:require [clojure.tools.macro :as macro]
|
||||||
|
[clojure.tools.namespace.repl :refer [disable-reload!]]
|
||||||
[clojure.tools.logging :refer [info]]))
|
[clojure.tools.logging :refer [info]]))
|
||||||
|
|
||||||
|
(disable-reload!)
|
||||||
|
|
||||||
|
(defonce ^:private session-id (System/currentTimeMillis))
|
||||||
|
(defonce ^:private state-seq (atom 0))
|
||||||
|
(defonce ^:private state-order (atom {}))
|
||||||
|
|
||||||
|
(defn- make-state-seq [state]
|
||||||
|
(or (@state-order state)
|
||||||
|
(let [nseq (swap! state-seq inc)]
|
||||||
|
(swap! state-order assoc state nseq)
|
||||||
|
nseq)))
|
||||||
|
|
||||||
;;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
|
||||||
|
|
@ -9,9 +22,13 @@
|
||||||
{:start start :stop stop})
|
{:start start :stop stop})
|
||||||
|
|
||||||
(defmacro defstate [state & body]
|
(defmacro defstate [state & body]
|
||||||
|
(info "calling defstate: " state ", body: " 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)) :started? true}
|
(let [s-meta (-> {:session-id session-id
|
||||||
|
:order (make-state-seq 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)))))
|
||||||
|
|
@ -30,7 +47,7 @@
|
||||||
(->> (all-ns)
|
(->> (all-ns)
|
||||||
(mapcat ns-interns)
|
(mapcat ns-interns)
|
||||||
(map second)
|
(map second)
|
||||||
(filter #(:state (meta %)))
|
(filter #(= (:session-id (meta %)) session-id))
|
||||||
(map #(f % (meta %)))))
|
(map #(f % (meta %)))))
|
||||||
|
|
||||||
(defn start []
|
(defn start []
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue