Is there a way to get all the clock keys in clojure

If there was an atom:

(def a (atom {})) 

with the following watch sets

 (add-watch a :watcher println) (add-watch a :watcher2 println) 

is there such a function?

 (get-watches a) ;; => [:watcher :watcher2] 
+7
source share
3 answers

(atom {}) creates an object of type clojure.lang.Atom , which extends the abstract class clojure.lang.ARef , which implements clojure.lang.IRef . IRef declares a getWatches method 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 does not have get-watches . Mirroring add-watch we get:

 (defn get-watches "Returns list of keys corresponding to watchers of the reference." [^clojure.lang.IRef reference] (keys (.getWatches reference))) 
+10
source
 (:watches (bean a)) 

or

 (keys (:watches (bean a))) 
+2
source

Ivan's answer is great for Clojure on the JVM. Here's how you do it in ClojureScript:

(keys (.-watches a))

+2
source

All Articles