adding "defstate!" wrapper (per @DomKM)
This commit is contained in:
parent
c1be3c377e
commit
eeda080322
2 changed files with 51 additions and 42 deletions
|
|
@ -53,25 +53,25 @@
|
||||||
(swap! running dissoc state)))
|
(swap! running dissoc state)))
|
||||||
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(defn current-state [state]
|
(defn current-state [state]
|
||||||
(let [{:keys [inst var]} (@meta-state state)]
|
(let [{:keys [inst var]} (@meta-state state)]
|
||||||
(if (= @mode :cljc)
|
(if (= @mode :cljc)
|
||||||
@inst
|
@inst
|
||||||
(var-get var))))
|
(var-get var))))
|
||||||
|
|
||||||
:cljs
|
:cljs
|
||||||
(defn current-state [state]
|
(defn current-state [state]
|
||||||
(-> (@meta-state state) :inst deref)))
|
(-> (@meta-state state) :inst deref)))
|
||||||
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(defn alter-state! [{:keys [var inst]} value]
|
(defn alter-state! [{:keys [var inst]} value]
|
||||||
(if (= @mode :cljc)
|
(if (= @mode :cljc)
|
||||||
(reset! inst value)
|
(reset! inst value)
|
||||||
(alter-var-root var (constantly value))))
|
(alter-var-root var (constantly value))))
|
||||||
|
|
||||||
:cljs
|
:cljs
|
||||||
(defn alter-state! [{:keys [inst]} value]
|
(defn alter-state! [{:keys [inst]} value]
|
||||||
(reset! inst value)))
|
(reset! inst value)))
|
||||||
|
|
||||||
(defn- update-meta! [path v]
|
(defn- update-meta! [path v]
|
||||||
(swap! meta-state assoc-in path v))
|
(swap! meta-state assoc-in path v))
|
||||||
|
|
@ -127,25 +127,34 @@
|
||||||
@inst)))
|
@inst)))
|
||||||
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(defmacro defstate [state & body]
|
(defmacro defstate [state & body]
|
||||||
(let [[state params] (macro/name-with-attributes state body)
|
(let [[state params] (macro/name-with-attributes state body)
|
||||||
{:keys [start stop suspend resume] :as lifecycle} (apply hash-map params)
|
{:keys [start stop suspend resume] :as lifecycle} (apply hash-map params)
|
||||||
state-name (with-ns *ns* state)
|
state-name (with-ns *ns* state)
|
||||||
order (make-state-seq state-name)
|
order (make-state-seq state-name)
|
||||||
sym (str state)]
|
sym (str state)]
|
||||||
(validate lifecycle)
|
(validate lifecycle)
|
||||||
(cleanup-if-dirty state-name)
|
(cleanup-if-dirty state-name)
|
||||||
(let [s-meta (cond-> {:order order
|
(let [s-meta (cond-> {:order order
|
||||||
:start `(fn [] ~start)
|
:start `(fn [] ~start)
|
||||||
:status #{:stopped}}
|
:status #{:stopped}}
|
||||||
stop (assoc :stop `(fn [] ~stop))
|
stop (assoc :stop `(fn [] ~stop))
|
||||||
suspend (assoc :suspend `(fn [] ~suspend))
|
suspend (assoc :suspend `(fn [] ~suspend))
|
||||||
resume (assoc :resume `(fn [] ~resume)))]
|
resume (assoc :resume `(fn [] ~resume)))]
|
||||||
`(do
|
`(do
|
||||||
(def ~state (DerefableState. ~state-name))
|
(def ~state (DerefableState. ~state-name))
|
||||||
((var update-meta!) [~state-name] (assoc ~s-meta :inst (atom (NotStartedState. ~state-name))
|
((var update-meta!) [~state-name] (assoc ~s-meta :inst (atom (NotStartedState. ~state-name))
|
||||||
:var (var ~state)))
|
:var (var ~state)))
|
||||||
(var ~state))))))
|
(var ~state))))))
|
||||||
|
|
||||||
|
#?(:clj
|
||||||
|
(defmacro defstate! [state & {:keys [start! stop!]}]
|
||||||
|
(let [state-name (with-ns *ns* state)]
|
||||||
|
`(defstate ~state
|
||||||
|
:start (~'let [~state (mount/current-state ~state-name)]
|
||||||
|
~start!)
|
||||||
|
:stop (~'let [~state (mount/current-state ~state-name)]
|
||||||
|
~stop!)))))
|
||||||
|
|
||||||
(defn in-cljc-mode []
|
(defn in-cljc-mode []
|
||||||
(reset! mode :cljc))
|
(reset! mode :cljc))
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,18 @@
|
||||||
#?(:cljs (:require-macros [mount.tools.macro])))
|
#?(:cljs (:require-macros [mount.tools.macro])))
|
||||||
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(defmacro on-error [msg f]
|
(defmacro on-error [msg f]
|
||||||
`(try
|
`(try
|
||||||
~f
|
~f
|
||||||
(catch #?(:clj Throwable
|
(catch #?(:clj Throwable
|
||||||
:cljs :default) t#
|
:cljs :default) t#
|
||||||
(throw #?(:clj (RuntimeException. ~msg t#)
|
(throw #?(:clj (RuntimeException. ~msg t#)
|
||||||
:cljs (js/Error (str ~msg (.-stack t#)))))))))
|
:cljs (js/Error (str ~msg (.-stack t#)))))))))
|
||||||
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(defmacro throw-runtime [msg]
|
(defmacro throw-runtime [msg]
|
||||||
`(throw #?(:clj (RuntimeException. ~msg)
|
`(throw #?(:clj (RuntimeException. ~msg)
|
||||||
:cljs (js/Error (str ~msg))))))
|
:cljs (js/Error (str ~msg))))))
|
||||||
|
|
||||||
;; this is a one to one copy from https://github.com/clojure/tools.macro
|
;; this is a one to one copy from https://github.com/clojure/tools.macro
|
||||||
;; to avoid a lib dependency for a single function
|
;; to avoid a lib dependency for a single function
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue