2016-08-07 12:01:34 +00:00
|
|
|
(ns $PROJECT_NAME_HYPHENATED$.events
|
2016-02-25 20:05:46 +00:00
|
|
|
(:require
|
2016-10-03 16:43:09 +00:00
|
|
|
[re-frame.core :refer [reg-event-db after]]
|
2017-05-12 13:38:37 +00:00
|
|
|
[clojure.spec.alpha :as s]
|
2016-10-01 21:58:41 +00:00
|
|
|
[$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."
|
2016-10-31 17:44:24 +00:00
|
|
|
[spec db [event]]
|
2016-07-24 22:06:55 +00:00
|
|
|
(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)))))
|
2016-02-25 20:05:46 +00:00
|
|
|
|
2016-10-01 21:58:41 +00:00
|
|
|
(def validate-spec
|
2016-10-03 16:43:09 +00:00
|
|
|
(if goog.DEBUG
|
|
|
|
(after (partial check-and-throw ::db/app-db))
|
|
|
|
[]))
|
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)))
|