Further cleanup around fix for #78

This commit is contained in:
mike-thompson-day8 2015-07-14 00:54:28 +10:00
parent cd46a22c8e
commit 2ba8914d8d
2 changed files with 19 additions and 19 deletions

View File

@ -8,20 +8,20 @@
;; This is a Prismatic Schema which documents the structure of app-db
;; See: https://github.com/Prismatic/schema
;;
;; The value in app-db should ALWAYS match this schema. The value in app-db
;; can ONLY be changed by event handlers so, after each handler has run, we
;; re-check that app-db still matches this schema.
;; The value in app-db should ALWAYS match this schema. Now, the value in
;; app-db can ONLY be changed by event handlers so, after each event handler
;; has run, we re-check that app-db still matches this schema.
;;
;; How is this done? Look in handlers.cljs and you'll notice that all handers
;; have an "after" middleware which does the re-check.
;; have an "after" middleware which does the schema re-check.
;;
;; None of this is strickly necessary. It could be omitted. But we find it
;; None of this is strictly necessary. It could be omitted. But we find it
;; good practice.
(def schema {;; a sorted-map is used to hold the todos.
:todos (s/both PersistentTreeMap ;; ensure sorted-map, not just map
;; each todos is keyed by its :id value
;; each todo is keyed by its integer :id value
{s/Int {:id s/Int :title s/Str :done s/Bool}})
;; controls what todos are shown/visible to the user
@ -43,8 +43,8 @@
;; -- Local Storage ----------------------------------------------------------
;;
;; Part of the todomvc challenge is to ongoingly store the todos in LocalStorage.
;; On app startup, must reload the todos from when the program was last run.
;; Part of the todomvc challenge is to store the todos in LocalStorage, and
;; on app startup, reload the todos from when the program was last run.
;; But not the setting for "showing" filter. Just the todos.
;;

View File

@ -8,21 +8,21 @@
;; -- Middleware --------------------------------------------------------------
;;
(defn schema-checker
"throw error with schema details if schema does not match.
For better errors, tag your schema with :ref metadata
e.g. (def my-schema ^{:ref (ns-line)} {...my schema....}"
[db]
(if-let [problems (s/check schema db)]
(throw (js/Error. (str "schema check failed: " problems)))))
(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 check-schema (after schema-checker))
;; after an event handler has run, this middleware can check that
;; it the value in app-db still correctly matches the schema.
(def check-schema-mw (after (partial check-and-throw schema)))
(def ->ls (after todos->ls!)) ;; middleware to store todos into local storage
;; middleware for any handler that manipulates todos
(def todo-middleware [check-schema ;; after ever event handler make sure the schema is still valid
(def todo-middleware [check-schema-mw ;; after ever event handler make sure the schema is still valid
(path :todos) ;; 1st param to handler will be value from this path
->ls ;; write to localstore each time
trim-v]) ;; remove event id from event vec
@ -43,14 +43,14 @@
;; usage: (dispatch [:initialise-db])
(register-handler ;; On app startup, ceate initial state
:initialise-db ;; event id being handled
check-schema
check-schema-mw
(fn [_ _] ;; the handler being registered
(merge default-value (ls->todos)))) ;; all hail the new state
(register-handler ;; this handler changes the footer filter
:set-showing ;; event-id
[check-schema (path :showing) trim-v] ;; middleware (wraps the handler)
[check-schema-mw (path :showing) trim-v] ;; middleware (wraps the handler)
;; Because of the path middleware above, the 1st parameter to
;; the handler below won't be the entire 'db', and instead will