re-natal/resources/cljs-reagent/handlers.cljs

36 lines
960 B
Plaintext
Raw Normal View History

2015-11-20 19:22:02 +00:00
(ns $PROJECT_NAME_HYPHENATED$.handlers
(:require
[re-frame.core :refer [register-handler after]]
2016-08-13 07:44:14 +00:00
[clojure.spec :as s]
[$PROJECT_NAME_HYPHENATED$.db :as db :refer [app-db]]))
2015-11-20 19:22:02 +00:00
;; -- Middleware ------------------------------------------------------------
;;
;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware
;;
(defn check-and-throw
"Throw an exception if db doesn't have a valid spec."
[spec db]
(when-not (s/valid? spec db)
(let [explain-data (s/explain-data spec db)]
(throw (ex-info (str "Spec check failed: " explain-data) explain-data)))))
(def validate-spec-mw
(if goog.DEBUG
(after (partial check-and-throw ::db/app-db))
[]))
;; -- Handlers --------------------------------------------------------------
2015-11-20 19:22:02 +00:00
(register-handler
:initialize-db
validate-spec-mw
2015-11-20 19:22:02 +00:00
(fn [_ _]
app-db))
(register-handler
:set-greeting
validate-spec-mw
2015-11-20 19:22:02 +00:00
(fn [db [_ value]]
(assoc db :greeting value)))