If there was an atom:
(def a (atom {}))
with the following watches set
(add-watch a :watcher println)
(add-watch a :watcher2 println)
is there a function like this?
(get-watches a)
;; => [:watcher :watcher2]
(atom {})
creates an object of type clojure.lang.Atom
which extends abstract class clojure.lang.ARef which implements clojure.lang.IRef interface. IRef declares method getWatches that is implemented in ARef.
Here's the solution:
(def a (atom {}))
(add-watch a :watcher println)
(println (-> a .getWatches keys))
It is strange that clojure.core
doesn't have get-watches
. Mirroring add-watch
implementation we get:
(defn get-watches
"Returns list of keys corresponding to watchers of the reference."
[^clojure.lang.IRef reference]
(keys (.getWatches reference)))
Ivan's answer is great for Clojure on the JVM. Here's how you do it in ClojureScript:
(keys (.-watches a))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With