2015-05-02 00:47:37 +00:00
|
|
|
(ns re-frame.utils
|
|
|
|
(:require
|
|
|
|
[clojure.set :refer [difference]]))
|
2014-12-15 11:56:32 +00:00
|
|
|
|
|
|
|
|
2015-04-26 21:38:37 +00:00
|
|
|
;; -- Logging -----------------------------------------------------------------
|
|
|
|
;;
|
2015-05-02 00:47:37 +00:00
|
|
|
;; re-frame internally uses a set of logging functions which, by default,
|
|
|
|
;; print to js/console.
|
|
|
|
;; Use set-loggers! if you want to change this default behaviour.
|
|
|
|
;; In production environment, you may want to capture exceptions and POST
|
|
|
|
;; them somewhere. to , you might want to override the way that exceptions are
|
|
|
|
;; handled by overridding "error"
|
2015-04-26 21:38:37 +00:00
|
|
|
;;
|
|
|
|
(def default-loggers
|
2015-05-02 00:47:37 +00:00
|
|
|
{:log #(.log js/console %)
|
|
|
|
:warn #(.warn js/console %)
|
|
|
|
:error #(.error js/console %)
|
2015-07-17 02:16:56 +00:00
|
|
|
:group #(if (.-groupCollapsed js/console) (.groupCollapsed js/console %) (.log js/console %)) ;; group does not exist < IE 11
|
|
|
|
:groupEnd #(when (.-groupEnd js/console) (.groupEnd js/console))}) ;; groupEnd does not exist < IE 11
|
2015-02-22 11:28:40 +00:00
|
|
|
|
2015-04-26 21:38:37 +00:00
|
|
|
;; holds the current set of loggers.
|
|
|
|
(def loggers (atom default-loggers))
|
2015-03-02 12:06:02 +00:00
|
|
|
|
2015-04-26 21:38:37 +00:00
|
|
|
(defn set-loggers!
|
2015-05-02 00:47:37 +00:00
|
|
|
"Change the set (subset?) of logging functions used by re-frame.
|
|
|
|
'new-loggers' should be a map which looks like default-loggers"
|
2015-04-26 21:38:37 +00:00
|
|
|
[new-loggers]
|
2015-05-02 00:47:37 +00:00
|
|
|
(assert (empty? (difference (set (keys new-loggers)) (set (keys default-loggers)))) "Unknown keys in new-loggers")
|
2015-04-26 21:38:37 +00:00
|
|
|
(swap! loggers merge new-loggers))
|
2015-03-02 12:06:02 +00:00
|
|
|
|
|
|
|
|
2015-05-02 00:47:37 +00:00
|
|
|
(defn log [& args] ((:log @loggers) (apply str args)))
|
|
|
|
(defn warn [& args] ((:warn @loggers) (apply str args)))
|
|
|
|
(defn group [& args] ((:group @loggers) (apply str args)))
|
|
|
|
(defn groupEnd [& args] ((:groupEnd @loggers) (apply str args)))
|
|
|
|
(defn error [& args] ((:error @loggers) (apply str args)))
|
2015-04-26 21:38:37 +00:00
|
|
|
|
|
|
|
;; -- Misc --------------------------------------------------------------------
|
2015-02-22 11:28:40 +00:00
|
|
|
|
2015-03-04 13:00:36 +00:00
|
|
|
(defn first-in-vector
|
2014-12-15 11:56:32 +00:00
|
|
|
[v]
|
2015-02-22 11:28:40 +00:00
|
|
|
(if (vector? v)
|
|
|
|
(first v)
|
2016-05-29 12:48:18 +00:00
|
|
|
(error "re-frame: expected a vector, but got: " v)))
|
2015-04-26 21:38:37 +00:00
|
|
|
|