Skip to content

Commit

Permalink
[new] [Signals] Add with-middleware+
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Oct 11, 2024
1 parent 973f677 commit 44d1e72
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
15 changes: 11 additions & 4 deletions src/taoensso/encore.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
34 changes: 23 additions & 11 deletions src/taoensso/encore/signals.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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*")]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion test/taoensso/encore_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 44d1e72

Please sign in to comment.