re-frame/src/re_frame/utils.cljs

50 lines
1.8 KiB
Plaintext
Raw Normal View History

2015-05-02 00:47:37 +00:00
(ns re-frame.utils
(:require
2016-05-30 21:50:49 +00:00
[clojure.set :refer [difference]]))
2014-12-15 11:56:32 +00:00
2015-04-26 21:38:37 +00:00
;; -- Logging -----------------------------------------------------------------
;;
2016-05-30 21:50:49 +00:00
;; re-frame uses a set of logging functions which are, by default, those from
;; js/console.
2015-05-02 00:47:37 +00:00
;; Use set-loggers! if you want to change this default behaviour.
2015-04-26 21:38:37 +00:00
;;
2016-05-30 21:50:49 +00:00
(def default-log (js/console.log.bind js/console))
(def default-warn (js/console.warn.bind js/console))
(def default-error (js/console.error.bind js/console))
(def default-group (if (.-group js/console) (js/console.group.bind js/console) default-log)) ;; group does not exist < IE 11
(def default-groupEnd (if (.-groupEnd js/console) (js/console.groupEnd.bind js/console) #())) ;; groupEnd does not exist < IE 11
2015-04-26 21:38:37 +00:00
(def default-loggers
2016-05-30 21:50:49 +00:00
{:log #(apply default-log %)
:warn #(apply default-warn %)
:error #(apply default-error %)
:group #(apply default-group %)
:groupEnd #(apply default-groupEnd %)})
2015-02-22 11:28:40 +00:00
2016-05-30 21:50:49 +00:00
;; holds the current set of loggin functions.
2015-04-26 21:38:37 +00:00
(def loggers (atom default-loggers))
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))
2016-05-30 21:50:49 +00:00
(defn log [& args] ((:log @loggers) args))
(defn warn [& args] ((:warn @loggers) args))
(defn group [& args] ((:group @loggers) args))
(defn groupEnd [& args] ((:groupEnd @loggers) args))
(defn error [& args] ((:error @loggers) args))
2015-04-26 21:38:37 +00:00
;; -- Misc --------------------------------------------------------------------
2015-02-22 11:28:40 +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