From 8cedfaa60af6214219b27b58d8f9ec43833861d7 Mon Sep 17 00:00:00 2001 From: anatoly Date: Tue, 29 Dec 2015 16:20:33 -0500 Subject: [PATCH] #25, #26 [fix for cljs]: cleaning states inside the macro body --- .gitignore | 2 ++ build.boot | 13 +++++++++++++ dev/cljs/app/example.cljs | 2 +- src/mount/core.cljc | 26 ++++++++++++++------------ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index ded85c2..10ab477 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ doo-index.html /.idea /.lein-repl-history /.nrepl-history +.cljs_rhino_repl/ +out/ diff --git a/build.boot b/build.boot index 526b0df..fbf893f 100644 --- a/build.boot +++ b/build.boot @@ -88,6 +88,19 @@ (comp (cljs :optimizations :advanced :ids #{"mount"}))) +(deftask cljs-dev + "mount cljs dev example" + [] + (set-env! :source-paths #(conj % "dev/clj" "dev/cljs")) + (set-env! :resource-paths #{"dev/resources"}) + + (comp + (serve :dir "dev/resources/public/") + (watch) + (reload) + (cljs-repl) + (cljs :optimizations :none :ids #{"mount"}))) + (deftask cljs-example "mount cljs example" [] diff --git a/dev/cljs/app/example.cljs b/dev/cljs/app/example.cljs index 2b9b475..320efa4 100644 --- a/dev/cljs/app/example.cljs +++ b/dev/cljs/app/example.cljs @@ -19,7 +19,7 @@ (mount/start) ;; time to establish a websocket connection before disconnecting -(js/setTimeout #(mount/stop-except "#'app.audit-log/log") 500) +;; (js/setTimeout #(mount/stop-except "#'app.audit-log/log") 500) ;; time to close a connection to show it in audit (js/setTimeout #(show-log) 1000) diff --git a/src/mount/core.cljc b/src/mount/core.cljc index 6c991ee..9066acb 100644 --- a/src/mount/core.cljc +++ b/src/mount/core.cljc @@ -53,8 +53,7 @@ (when stop (prn (str "<< stopping.. " state " (namespace was recompiled)")) (stop)) - (swap! running dissoc state) - {:restart? true})) + (swap! running dissoc state))) #?(:clj (defn current-state [state] @@ -130,6 +129,9 @@ (up name state (atom #{}))) @inst))) + ;; #?(:clj + ;; (println (str "[mount]: here is what I see running: " @@#'mount.core/running + ;; " is " state " running? [" @@#'mount.core/running state))) #?(:clj (defmacro defstate [state & body] (let [[state params] (macro/name-with-attributes state body) @@ -138,22 +140,22 @@ order (make-state-seq state-name) sym (str state)] (validate lifecycle) - (let [{:keys [restart?]} (cleanup-if-dirty state-name) - s-meta (cond-> {:order order + (let [s-meta (cond-> {:order order :start `(fn [] ~start) :status #{:stopped}} stop (assoc :stop `(fn [] ~stop)) suspend (assoc :suspend `(fn [] ~suspend)) resume (assoc :resume `(fn [] ~resume)))] `(do - (defonce ~state (DerefableState. ~state-name)) - (let [meta# (assoc ~s-meta :inst (atom (NotStartedState. ~state-name)) - :var (var ~state))] - ((var mount.core/update-meta!) [~state-name] meta#) - (when ~restart? - (prn (str ">> starting.. " ~state-name " (namespace was recompiled)")) - ((var mount.core/up) ~state-name meta# (atom #{}))) - (var ~state))))))) + (~'defonce ~state (DerefableState. ~state-name)) + (let [meta# (~'assoc ~s-meta :inst (~'atom (NotStartedState. ~state-name)) + :var (~'var ~state)) + restart?# ((~'var mount.core/cleanup-if-dirty) ~state-name)] + ((~'var mount.core/update-meta!) [~state-name] meta#) + (~'when restart?# + (~'prn (str ">> starting.. " ~state-name " (namespace was recompiled)")) + ((~'var mount.core/up) ~state-name meta# (~'atom #{}))) + (~'var ~state))))))) #?(:clj (defmacro defstate! [state & {:keys [start! stop!]}]