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
|
(ns re-frame.middleware
|
||||||
(:require
|
(:require
|
||||||
[reagent.ratom :refer [IReactiveAtom]]
|
[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
|
;; 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
|
(defn pure
|
||||||
"Middleware which allows you to write a pure handler.
|
"Middleware which adapts a pure handler to the standard calling convension"
|
||||||
1. on the way through it extracts the value in the atom
|
[handler]
|
||||||
2. resets the atom with the returned value after calling the handler"
|
(fn new-handler
|
||||||
[next-handler]
|
|
||||||
(fn handler
|
|
||||||
[app-db event-vec]
|
[app-db event-vec]
|
||||||
(assert (satisfies? IReactiveAtom app-db) "re-frame: make-pure not given a Ratom")
|
(assert (satisfies? IReactiveAtom app-db)
|
||||||
(reset! app-db (next-handler @app-db event-vec))))
|
(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
|
#_(defn check-schema
|
||||||
"Middleware for checking that a handlers mutations leave the state in a schema-matching way"
|
"Middleware for checking that a handlers mutations leave the state in a schema-matching way"
|
||||||
[a-prismatic-schema]
|
[a-prismatic-schema]
|
||||||
|
@ -39,19 +40,54 @@
|
||||||
(let [val (next-handler db v)
|
(let [val (next-handler db v)
|
||||||
valid? true] ;; XXXXX replace true by code which checks the schema using original parameter
|
valid? true] ;; XXXXX replace true by code which checks the schema using original parameter
|
||||||
(if (not valid?)
|
(if (not valid?)
|
||||||
(warn "re-frame: schema not valid after:" ))
|
(warn "re-frame: schema not valid after:" v))
|
||||||
val))))
|
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
|
(defn validate
|
||||||
"Middleware that applies a validation function to the db after the handler is finished.
|
"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.
|
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"
|
By validation, I mean validation of what the user has entered, or the state they have taken the app too"
|
||||||
[f]
|
[f]
|
||||||
(fn middleware
|
(fn middleware
|
||||||
[next-handler]
|
[handler]
|
||||||
(fn handler
|
(fn new-handler
|
||||||
[db v]
|
[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