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

34 lines
936 B
Clojure

(ns $PROJECT_NAME_HYPHENATED$.handlers
(:require
[re-frame.core :refer [register-handler after]]
[schema.core :as s :include-macros true]
[$PROJECT_NAME_HYPHENATED$.db :refer [app-db schema]]))
;; -- 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]
(when-let [problems (s/check a-schema db)]
(throw (js/Error. (str "schema check failed: " problems)))))
(def validate-schema-mw
(if goog.DEBUG
(after (partial check-and-throw schema))
[]))
;; -- Handlers --------------------------------------------------------------
(register-handler
:initialize-db
validate-schema-mw
(fn [_ _]
app-db))
(register-handler
:set-greeting
validate-schema-mw
(fn [db [_ value]]
(assoc db :greeting value)))