forked from dmotz/natal
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
follow best practices of re-frame and give an app-db a schema
- Loading branch information
1 parent
bef4f05
commit da97f17
Showing
4 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(ns $PROJECT_NAME_HYPHENATED$.db | ||
(:require [schema.core :as s :include-macros true])) | ||
|
||
;; schema of app-db | ||
(def schema {:greeting s/Str}) | ||
|
||
;; initial state of app-db | ||
(def app-db {:greeting "Hello Clojure in iOS and Android!"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,32 @@ | ||
(ns $PROJECT_NAME_HYPHENATED$.handlers | ||
(:require | ||
[re-frame.core :refer [register-handler path trim-v after dispatch]])) | ||
[re-frame.core :refer [register-handler after]] | ||
[schema.core :as s :include-macros true] | ||
[$PROJECT_NAME_HYPHENATED$.db :refer [app-db schema]])) | ||
|
||
(def app-db {:greeting "Hello Clojure in iOS and Android!"}) | ||
;; -- 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 -------------------------------------------------------------- | ||
|
||
(register-handler | ||
:initialize-db | ||
validate-schema-mw | ||
(fn [_ _] | ||
app-db)) | ||
|
||
(register-handler | ||
:set-greeting | ||
validate-schema-mw | ||
(fn [db [_ value]] | ||
(assoc db :greeting value))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters