2015-11-20 19:22:02 +00:00
|
|
|
(ns $PROJECT_NAME_HYPHENATED$.handlers
|
|
|
|
(:require
|
2015-12-13 21:49:54 +00:00
|
|
|
[re-frame.core :refer [register-handler after]]
|
|
|
|
[schema.core :as s :include-macros true]
|
|
|
|
[$PROJECT_NAME_HYPHENATED$.db :refer [app-db schema]]))
|
2015-11-20 19:22:02 +00:00
|
|
|
|
2015-12-13 21:49:54 +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 match the schema."
|
|
|
|
[a-schema db]
|
|
|
|
(if-let [problems (s/check a-schema db)]
|
|
|
|
(throw (js/Error. (str "schema check failed: " problems)))))
|
|
|
|
|
|
|
|
(def validate-schema-mw
|
|
|
|
(after (partial check-and-throw schema)))
|
|
|
|
|
|
|
|
;; -- Handlers --------------------------------------------------------------
|
2015-11-20 19:22:02 +00:00
|
|
|
|
|
|
|
(register-handler
|
|
|
|
:initialize-db
|
2015-12-13 21:49:54 +00:00
|
|
|
validate-schema-mw
|
2015-11-20 19:22:02 +00:00
|
|
|
(fn [_ _]
|
|
|
|
app-db))
|
|
|
|
|
|
|
|
(register-handler
|
|
|
|
:set-greeting
|
2015-12-13 21:49:54 +00:00
|
|
|
validate-schema-mw
|
2015-11-20 19:22:02 +00:00
|
|
|
(fn [db [_ value]]
|
|
|
|
(assoc db :greeting value)))
|