re-natal/resources/cljs-reagent6/events.cljs

36 lines
961 B
Plaintext
Raw Normal View History

(ns $PROJECT_NAME_HYPHENATED$.events
(:require
[re-frame.core :refer [reg-event-db after]]
[clojure.spec.alpha :as s]
[$PROJECT_NAME_HYPHENATED$.db :as db :refer [app-db]]))
;; -- Interceptors ------------------------------------------------------------
;;
;; See https://github.com/Day8/re-frame/blob/master/docs/Interceptors.md
;;
(defn check-and-throw
"Throw an exception if db doesn't have a valid spec."
2016-10-31 17:44:24 +00:00
[spec db [event]]
(when-not (s/valid? spec db)
(let [explain-data (s/explain-data spec db)]
2016-10-31 17:44:24 +00:00
(throw (ex-info (str "Spec check after " event " failed: " explain-data) explain-data)))))
(def validate-spec
(if goog.DEBUG
(after (partial check-and-throw ::db/app-db))
[]))
;; -- Handlers --------------------------------------------------------------
(reg-event-db
:initialize-db
validate-spec
(fn [_ _]
app-db))
(reg-event-db
:set-greeting
validate-spec
(fn [db [_ value]]
(assoc db :greeting value)))