Skip to content

Commit

Permalink
[mod] NB Change return value of experimental with-signals
Browse files Browse the repository at this point in the history
The old (deeply-nested) return value seemed to be difficult
for users to read, and prone to mistakes when destructuring.

The new (map) return value is a little more verbose, but
more obvious and less error-prone. Overall a good trade-off
given that this util is anyway used mostly for debugging
or unit tests.
  • Loading branch information
ptaoussanis committed Dec 23, 2024
1 parent 5c977a3 commit cb6a5d9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 110 deletions.
2 changes: 1 addition & 1 deletion projects/main/src/taoensso/telemere.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,4 @@
(do (let [hf (handler:console)] (hf sig) (hf)))
#?(:cljs (let [hf (handler:console-raw)] (hf sig) (hf)))))

(comment (let [[_ [s1 s2]] (with-signals (trace! ::id1 (trace! ::id2 "form2")))] s1))
(comment (let [{[s1 s2] :signals} (with-signals (trace! ::id1 (trace! ::id2 "form2")))] s1))
27 changes: 17 additions & 10 deletions projects/main/src/taoensso/telemere/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -327,24 +327,31 @@
#?(:clj
(defmacro ^:public with-signals
"Experimental, subject to change.
Like `with-signal` but returns [[<form-value> <form-error>] [<signal1> ...]].
Useful for more advanced tests/debugging."
Like `with-signal` but returns {:keys [value error signals]}.
Useful for more advanced tests/debugging.
Destructuring example:
(let [{:keys [value error] [sig1 sig2] :signals} (with-signals ...)]
...)"
([ form] `(with-signals false false ~form))
([ trap-signals? form] `(with-signals false ~trap-signals? ~form))
([raw-msgs? trap-signals? form]
`(let [sigs_# (volatile! nil)
form-result#
base-map#
(binding [*sig-spy* (SpyOpts. sigs_# false ~trap-signals?)]
(enc/try*
(do [~form nil])
(catch :all t# [nil t#])))
(do {:value ~form})
(catch :all t# {:error t#})))

sigs#
(if ~raw-msgs?
(do @sigs_#)
(mapv force-msg-in-sig @sigs_#))]

[form-result# (not-empty sigs#)]))))
(not-empty
(if ~raw-msgs?
(do @sigs_#)
(mapv force-msg-in-sig @sigs_#)))]

(if sigs#
(assoc base-map# :signals sigs#)
(do base-map#))))))

#?(:clj (def ^:dynamic *sig-spy-off-thread?* false))
(defn dispatch-signal!
Expand Down
2 changes: 1 addition & 1 deletion projects/main/src/taoensso/telemere/open_telemetry.clj
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
(do
(require '[taoensso.telemere :as t])
(def h1 (handler:open-telemetry))
(let [[_ [s1 s2]] (t/with-signals (t/trace! ::id1 (t/trace! ::id2 "form2")))]
(let [{[s1 s2] :signals} (t/with-signals (t/trace! ::id1 (t/trace! ::id2 "form2")))]
(def s1 s1)
(def s2 s2)))

Expand Down
Loading

0 comments on commit cb6a5d9

Please sign in to comment.