diff --git a/src/taoensso/encore.cljc b/src/taoensso/encore.cljc index 9540767c..2cfe8ecf 100644 --- a/src/taoensso/encore.cljc +++ b/src/taoensso/encore.cljc @@ -7102,10 +7102,17 @@ Useful for composing Ring-style middleware fns." {:added "Encore v3.106.0 (2024-05-01)"} - ([fs ] (fn [x] (reduce (fn [x f] (or (f x) (reduced nil))) x fs))) - ([f1 f2 ] (fn [x] (when-let [x (f1 x) ] (f2 x)))) - ([f1 f2 f3 ] (fn [x] (when-let [x (f1 x), x (f2 x) ] (f3 x)))) - ([f1 f2 f3 & fs] (fn [x] (when-let [x (f1 x), x (f2 x), x (f3 x)] ((comp-middleware fs) x))))) + ([fs ] (fn [x] (reduce (fn [x f] (if f (or (f x) (reduced nil)) x)) x fs))) + ([f1 f2] + (fn [x] + (if f1 + (if f2 + (when-let [x (f1 x)] (f2 x)) + (do (f1 x))) + (if f2 (f2 x) x)))) + + ([f1 f2 f3 ] (fn [x] (when-let [x (if f1 (f1 x) x), x (if f2 (f2 x) x) ] (if f3 (f3 x) x)))) + ([f1 f2 f3 & fs] (fn [x] (when-let [x (if f1 (f1 x) x), x (if f2 (f2 x) x), x (if f3 (f3 x) x)] ((comp-middleware fs) x))))) (comment ((comp-middleware inc inc (fn [_] nil) (fn [_] (throw (Exception. "Foo")))) 0)) diff --git a/src/taoensso/encore/signals.cljc b/src/taoensso/encore/signals.cljc index f0160caf..2201df59 100644 --- a/src/taoensso/encore/signals.cljc +++ b/src/taoensso/encore/signals.cljc @@ -1487,7 +1487,7 @@ Useful for dynamically transforming signals and/or filtering signals by signal data/content/etc. - Re/bind dynamic value using `with-middleware`, `binding`. + Re/bind dynamic value using `with-middleware`, `with-middleware+`, `binding`. Modify root (default) value using `set-middleware!`. As with all dynamic Clojure vars, \"binding conveyance\" applies when using @@ -1547,15 +1547,6 @@ "Evaluates given form with given `*ctx*` value. See `*ctx*` for details." ~'[ctx-val form] `(binding [~'~*ctx* ~~'ctx-val] ~~'form))))) -#?(:clj - (defn- api:with-middleware [] - (let [*middleware* (symbol (str *ns*) "*middleware*")] - `(defmacro ~'with-middleware - "Evaluates given form with given `*middleware*` value. - See `*middleware*` for details." - ~'[?middleware-fn form] - `(binding [~'~*middleware* ~~'?middleware-fn] ~~'form))))) - #?(:clj (defn- api:with-ctx+ [] (let [*ctx* (symbol (str *ns*) "*ctx*")] @@ -1570,6 +1561,26 @@ ~'[update-map-or-fn form] `(binding [~'~*ctx* (update-ctx ~'~*ctx* ~~'update-map-or-fn)] ~~'form))))) +#?(:clj + (defn- api:with-middleware [] + (let [*middleware* (symbol (str *ns*) "*middleware*")] + `(defmacro ~'with-middleware + "Evaluates given form with given `*middleware*` value. + See `*middleware*` for details." + ~'[?middleware-fn form] + `(binding [~'~*middleware* ~~'?middleware-fn] ~~'form))))) + +#?(:clj + (defn- api:with-middleware+ [] + (let [*middleware* (symbol (str *ns*) "*middleware*")] + `(defmacro ~'with-middleware+ + "Evaluates given form with composed `*middleware*` value. + Same as (with-middleware (comp-middleware *middleware* ?middleware-fn) ...). + See `*middleware*` for details." + ~'[?middleware-fn form] + `(binding [~'~*middleware* (enc/comp-middleware ~'~*middleware* ~~'?middleware-fn)] + ~~'form))))) + ;;;; #?(:clj @@ -1635,7 +1646,8 @@ ~(api:with-ctx) ~(api:with-ctx+) - ~(api:with-middleware))))) + ~(api:with-middleware) + ~(api:with-middleware+))))) (comment ;; See `taoensso.encore-tests.signal-api` ns diff --git a/test/taoensso/encore_tests.cljc b/test/taoensso/encore_tests.cljc index 4faddeed..b89dc597 100644 --- a/test/taoensso/encore_tests.cljc +++ b/test/taoensso/encore_tests.cljc @@ -985,7 +985,16 @@ (testing "`xn` short-circuits (skips `x!`)" [(is (= ((enc/comp-middleware inc xn x!) 0) nil)) (is (= ((enc/comp-middleware [inc xn x!]) 0) nil)) - (is (= ((enc/comp-middleware inc inc inc inc xn x!) 0) nil))])])) + (is (= ((enc/comp-middleware inc inc inc inc xn x!) 0) nil))]) + + (testing "nil fns" + [(is (= ((enc/comp-middleware [nil inc nil x2]) 0) 2)) + (is (= ((enc/comp-middleware nil x2 ) 1) 2)) + (is (= ((enc/comp-middleware x2 nil ) 1) 2)) + (is (= ((enc/comp-middleware x2 nil nil) 1) 2)) + (is (= ((enc/comp-middleware nil x2 nil) 1) 2)) + (is (= ((enc/comp-middleware nil nil x2 ) 1) 2)) + (is (= ((enc/comp-middleware nil x2 nil x2 nil inc) 1) 5))])])) ;;;; Futures