2016-08-07 12:01:34 +00:00
|
|
|
(ns $PROJECT_NAME_HYPHENATED$.events
|
2016-02-25 20:05:46 +00:00
|
|
|
(:require
|
2016-10-01 21:58:41 +00:00
|
|
|
[re-frame.core :refer [reg-event-db ->interceptor]]
|
|
|
|
[clojure.spec :as s]
|
|
|
|
[$PROJECT_NAME_HYPHENATED$.db :as db :refer [app-db]]))
|
2016-02-25 20:05:46 +00:00
|
|
|
|
2016-10-01 21:58:41 +00:00
|
|
|
;; -- Interceptors ------------------------------------------------------------
|
2016-02-25 20:05:46 +00:00
|
|
|
;;
|
2016-10-01 21:58:41 +00:00
|
|
|
;; See https://github.com/Day8/re-frame/blob/master/docs/Interceptors.md
|
2016-02-25 20:05:46 +00:00
|
|
|
;;
|
|
|
|
(defn check-and-throw
|
2016-07-24 22:06:55 +00:00
|
|
|
"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)))))
|
2016-02-25 20:05:46 +00:00
|
|
|
|
2016-10-01 21:58:41 +00:00
|
|
|
(def validate-spec
|
|
|
|
(->interceptor
|
|
|
|
:id :validate-spec
|
|
|
|
:after (fn [context]
|
|
|
|
(if goog.DEBUG
|
|
|
|
(check-and-throw ::db/app-db (get-in context [:effects :db])))
|
|
|
|
context)))
|
2016-02-25 20:05:46 +00:00
|
|
|
|
|
|
|
;; -- Handlers --------------------------------------------------------------
|
|
|
|
|
2016-08-28 11:28:05 +00:00
|
|
|
(reg-event-db
|
2016-10-01 21:58:41 +00:00
|
|
|
:initialize-db
|
|
|
|
validate-spec
|
|
|
|
(fn [_ _]
|
|
|
|
app-db))
|
2016-02-25 20:05:46 +00:00
|
|
|
|
2016-08-28 11:28:05 +00:00
|
|
|
(reg-event-db
|
2016-10-01 21:58:41 +00:00
|
|
|
:set-greeting
|
|
|
|
validate-spec
|
|
|
|
(fn [db [_ value]]
|
|
|
|
(assoc db :greeting value)))
|