New middleware, largely untested
This commit is contained in:
parent
44b50e4feb
commit
16b4efa3fd
|
@ -0,0 +1 @@
|
|||
(ns re-frame.core)
|
|
@ -1,34 +1,35 @@
|
|||
(ns re-frame.middleware
|
||||
(:require
|
||||
[reagent.ratom :refer [IReactiveAtom]]
|
||||
[re-frame.undo :refer [store-now!]]))
|
||||
[re-frame.undo :refer [store-now!]]
|
||||
[re-frame.utils :refer [warn]]))
|
||||
|
||||
|
||||
;; -- Middleware Factories -------------------------------------------------------------------------
|
||||
;;
|
||||
;; Read this: https://github.com/Day8/re-frame/wiki/Middleware
|
||||
;;
|
||||
|
||||
(defn undoable
|
||||
"Middleware which stores an undo checkpoint"
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[app-db event-vec]
|
||||
(store-now!)
|
||||
(next-handler app-db event-vec)))
|
||||
|
||||
|
||||
(defn pure
|
||||
"Middleware which allows you to write a pure handler.
|
||||
1. on the way through it extracts the value in the atom
|
||||
2. resets the atom with the returned value after calling the handler"
|
||||
[next-handler]
|
||||
(fn handler
|
||||
"Middleware which adapts a pure handler to the standard calling convension"
|
||||
[handler]
|
||||
(fn new-handler
|
||||
[app-db event-vec]
|
||||
(assert (satisfies? IReactiveAtom app-db) "re-frame: make-pure not given a Ratom")
|
||||
(reset! app-db (next-handler @app-db event-vec))))
|
||||
(assert (satisfies? IReactiveAtom app-db)
|
||||
(str "re-frame: pure not given a Ratom" (if (map? app-db) ". Perhaps \"pure\" is in twice.")))
|
||||
(reset! app-db (handler @app-db event-vec))))
|
||||
|
||||
|
||||
;; example of applying
|
||||
(defn undoable
|
||||
"Middleware which stores an undo checkpoint, prior to handler being called."
|
||||
[handler]
|
||||
(fn new-handler
|
||||
[app-db event-vec]
|
||||
(store-now!)
|
||||
(handler app-db event-vec)))
|
||||
|
||||
|
||||
|
||||
|
||||
;; check the state of db AFTER the handler has run, using a prismatic Schema.
|
||||
#_(defn check-schema
|
||||
"Middleware for checking that a handlers mutations leave the state in a schema-matching way"
|
||||
[a-prismatic-schema]
|
||||
|
@ -39,19 +40,54 @@
|
|||
(let [val (next-handler db v)
|
||||
valid? true] ;; XXXXX replace true by code which checks the schema using original parameter
|
||||
(if (not valid?)
|
||||
(warn "re-frame: schema not valid after:" ))
|
||||
(warn "re-frame: schema not valid after:" v))
|
||||
val))))
|
||||
|
||||
|
||||
(defn apply-event
|
||||
"Middleware which removes the first bit of v, and \"expands\" other parameters.
|
||||
Normally handlers get two paramters: db and v.
|
||||
With this middleware, if v was [:id 1 2], the handler would be called with db, 1, 2.
|
||||
Use the middleware in the very last place -- right-most in comp"
|
||||
[handler]
|
||||
(fn new-handler
|
||||
[db v]
|
||||
(apply handler (cons db (rest v)))))
|
||||
|
||||
|
||||
(defn path
|
||||
"Supplies a sub-tree of `app-db` to the handler.
|
||||
Assumes pure is
|
||||
Grafts the result back into."
|
||||
[p]
|
||||
(fn middleware
|
||||
[handler]
|
||||
(fn new-handler
|
||||
[db v]
|
||||
(warn (vector? p) "re-frame: ex")
|
||||
(assoc-in db p (handler db v)))))
|
||||
|
||||
|
||||
(defn validate
|
||||
"Middleware that applies a validation function to the db after the handler is finished.
|
||||
The validation function f, might assoc warnings and errors to the new state, created by the handler.
|
||||
By validation, I mean validation of what the user has entered, or the state they have taken the app too"
|
||||
[f]
|
||||
(fn middleware
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[handler]
|
||||
(fn new-handler
|
||||
[db v]
|
||||
(f (next-handler db v)))))
|
||||
(f (handler db v)))))
|
||||
|
||||
|
||||
(defn log-events
|
||||
"Middleware that logs events (vec) using to the given logger fucntion"
|
||||
[logger]
|
||||
(fn middleware
|
||||
[handler]
|
||||
(fn new-handler
|
||||
[db v]
|
||||
(logger v)
|
||||
(handler db v))))
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue