diff --git a/doc/img/cljs-ns-reload.png b/doc/img/cljs-ns-reload.png new file mode 100644 index 0000000..e71c2cf Binary files /dev/null and b/doc/img/cljs-ns-reload.png differ diff --git a/src/mount/core.cljc b/src/mount/core.cljc index 795075d..d872eb3 100644 --- a/src/mount/core.cljc +++ b/src/mount/core.cljc @@ -3,7 +3,7 @@ [clojure.string :as s]) :cljs (:require [mount.tools.macro :as macro])) #?(:cljs (:require-macros [mount.core] - [mount.tools.macro :refer [on-error throw-runtime]]))) + [mount.tools.macro :refer [if-clj on-error throw-runtime]]))) (defonce ^:private -args (atom :no-args)) ;; mostly for command line args and external files (defonce ^:private state-seq (atom 0)) @@ -51,7 +51,9 @@ [state] (when-let [{:keys [stop] :as up} (@running state)] (when stop - (prn (str "<< stopping.. " state " (namespace was recompiled)")) + (let [note (str "<< stopping.. " state " (namespace was recompiled)")] + #?(:clj (prn note) + :cljs (.log js/console note))) (stop)) (swap! running dissoc state))) @@ -86,7 +88,7 @@ (defn- up [state {:keys [start stop resume status] :as current} done] (when-not (:started status) - (let [s (on-error (str "could not start [" state "] due to") + (let [s (on-error (str "could not start [" state "] due to") (if (:suspended status) (record! state resume done) (record! state start done)))] @@ -129,6 +131,14 @@ (up name state (atom #{}))) @inst))) +#?(:clj + (defn log [msg] + (prn msg))) + +#?(:cljs + (defn log [msg] + (.log js/console msg))) + #?(:clj (defmacro defstate [state & body] (let [[state params] (macro/name-with-attributes state body) @@ -150,7 +160,7 @@ 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)")) + (log (~'str ">> starting.. " ~state-name " (namespace was recompiled)")) ((~'var mount.core/up) ~state-name meta# (~'atom #{}))) (~'var ~state))))))) diff --git a/src/mount/tools/macro.cljc b/src/mount/tools/macro.cljc index f7449d0..dc46e24 100644 --- a/src/mount/tools/macro.cljc +++ b/src/mount/tools/macro.cljc @@ -1,19 +1,26 @@ (ns mount.tools.macro #?(:cljs (:require-macros [mount.tools.macro]))) +#?(:clj + (defmacro if-clj [then else] + (if (-> &env :ns not) + then + else))) + #?(:clj (defmacro on-error [msg f] - `(try - ~f - (catch #?(:clj Throwable - :cljs :default) t# - (throw #?(:clj (RuntimeException. ~msg t#) - :cljs (js/Error (str ~msg (.-stack t#))))))))) + `(if-clj + (try ~f + (catch Throwable t# + (throw (RuntimeException. ~msg t#)))) + (try ~f + (catch :default t# + (throw (~'str ~msg " " t#))))))) #?(:clj (defmacro throw-runtime [msg] - `(throw #?(:clj (RuntimeException. ~msg) - :cljs (js/Error (str ~msg)))))) + `(throw (if-clj (RuntimeException. ~msg) + (~'str ~msg))))) ;; this is a one to one copy from https://github.com/clojure/tools.macro ;; to avoid a lib dependency for a single function