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])
: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)))))))

View file

@ -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