Middleware can now be supplied as a vector (containing nils, which are ignored). This is going to make generating middleware much easier.
For example, Debug middleware can now more easily be conditionally added and removed.
This commit is contained in:
parent
d896189503
commit
f5df77163b
|
@ -37,19 +37,4 @@
|
|||
(register-handler id [pure middleware] handler)))
|
||||
|
||||
|
||||
(defn comp-middleware
|
||||
"Given a vector of middleware, filter out any nils, and use comp to compose.
|
||||
v can be vectors of vectors and will be flattended before use.
|
||||
For convienience, if v turns out to already be a function, just return it."
|
||||
[v]
|
||||
(if (ifn? v) ;; noop if v is a fucntion
|
||||
v
|
||||
(do
|
||||
(assert (or (vector? v) (list v))
|
||||
(str "re-frame: compose expects a vector, got: " v))
|
||||
(let [v (remove nil? (flatten v))]
|
||||
(cond
|
||||
(empty? v) noop
|
||||
(= 1 (count v)) (first v)
|
||||
:else (apply comp v))))))
|
||||
|
||||
|
|
|
@ -7,6 +7,25 @@
|
|||
[cljs.core.async :refer [chan put! <! timeout]]))
|
||||
|
||||
|
||||
|
||||
;; -- composing middleware -----------------------------------------------------------------------
|
||||
|
||||
(defn comp-middleware
|
||||
"Given a vector of middleware, filter out any nils, and use \"comp\" to compose them.
|
||||
v can have nested vectors, and will be flattended before \"comp\" is applied.
|
||||
For convienience, if v turns out to already be a function (assumed to be
|
||||
middleware), just return it."
|
||||
[v]
|
||||
(cond
|
||||
(ifn? v) v ;; assumed to be existing middleware
|
||||
(vector? v) (let [v (remove nil? (flatten v))]
|
||||
(cond
|
||||
(empty? v) noop
|
||||
(= 1 (count v)) (first v)
|
||||
:else (apply comp v)))
|
||||
:else (assert (vector? v)
|
||||
(str "re-frame: compose expects a vector, got: " v))))
|
||||
|
||||
;; -- the register of event handlers --------------------------------------------------------------
|
||||
|
||||
(def ^:private id->fn (atom {}))
|
||||
|
@ -19,9 +38,7 @@
|
|||
(swap! id->fn assoc event-id handler-fn))
|
||||
|
||||
([event-id middleware handler-fn]
|
||||
(let [mware (if (vector? middleware)
|
||||
(apply comp middleware) ;; compose the vector of middleware
|
||||
middleware)
|
||||
(let [mware (comp-middleware middleware)
|
||||
hander-fn (mware handler-fn)]
|
||||
(register event-id hander-fn))))
|
||||
|
||||
|
|
Loading…
Reference in New Issue