refactoring reader conditionals out of cljs exceptions macro (thx @DomKM)

This commit is contained in:
anatoly 2015-12-30 00:55:39 -05:00
parent 64a91625aa
commit a0d312fbd4
3 changed files with 29 additions and 12 deletions

BIN
doc/img/cljs-ns-reload.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

View file

@ -3,7 +3,7 @@
[clojure.string :as s]) [clojure.string :as s])
:cljs (:require [mount.tools.macro :as macro])) :cljs (:require [mount.tools.macro :as macro]))
#?(:cljs (:require-macros [mount.core] #?(: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 -args (atom :no-args)) ;; mostly for command line args and external files
(defonce ^:private state-seq (atom 0)) (defonce ^:private state-seq (atom 0))
@ -51,7 +51,9 @@
[state] [state]
(when-let [{:keys [stop] :as up} (@running state)] (when-let [{:keys [stop] :as up} (@running state)]
(when stop (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)) (stop))
(swap! running dissoc state))) (swap! running dissoc state)))
@ -86,7 +88,7 @@
(defn- up [state {:keys [start stop resume status] :as current} done] (defn- up [state {:keys [start stop resume status] :as current} done]
(when-not (:started status) (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) (if (:suspended status)
(record! state resume done) (record! state resume done)
(record! state start done)))] (record! state start done)))]
@ -129,6 +131,14 @@
(up name state (atom #{}))) (up name state (atom #{})))
@inst))) @inst)))
#?(:clj
(defn log [msg]
(prn msg)))
#?(:cljs
(defn log [msg]
(.log js/console msg)))
#?(: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)
@ -150,7 +160,7 @@
restart?# ((~'var mount.core/cleanup-if-dirty) ~state-name)] restart?# ((~'var mount.core/cleanup-if-dirty) ~state-name)]
((~'var mount.core/update-meta!) [~state-name] meta#) ((~'var mount.core/update-meta!) [~state-name] meta#)
(~'when restart?# (~'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 mount.core/up) ~state-name meta# (~'atom #{})))
(~'var ~state))))))) (~'var ~state)))))))

View file

@ -1,19 +1,26 @@
(ns mount.tools.macro (ns mount.tools.macro
#?(:cljs (:require-macros [mount.tools.macro]))) #?(:cljs (:require-macros [mount.tools.macro])))
#?(:clj
(defmacro if-clj [then else]
(if (-> &env :ns not)
then
else)))
#?(:clj #?(:clj
(defmacro on-error [msg f] (defmacro on-error [msg f]
`(try `(if-clj
~f (try ~f
(catch #?(:clj Throwable (catch Throwable t#
:cljs :default) t# (throw (RuntimeException. ~msg t#))))
(throw #?(:clj (RuntimeException. ~msg t#) (try ~f
:cljs (js/Error (str ~msg (.-stack t#))))))))) (catch :default t#
(throw (~'str ~msg " " t#)))))))
#?(:clj #?(:clj
(defmacro throw-runtime [msg] (defmacro throw-runtime [msg]
`(throw #?(:clj (RuntimeException. ~msg) `(throw (if-clj (RuntimeException. ~msg)
:cljs (js/Error (str ~msg)))))) (~'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