Skip to main content

add-watch

functionv1.7

cljs.core/add-watch

Available in:BBCLJCLJS
(add-watch [iref key f])
Adds a watch function to an atom reference. The watch fn must be a fn of 4 args: a key, the reference, its old-state, its new-state. Whenever the reference's state might have been changed, any registered watches will have their functions called. The watch fn will be called synchronously. Note that an atom's state may have changed again prior to the fn call, so use old/new-state rather than derefing the reference. Keys must be unique per reference, and can be used to remove the watch with remove-watch, but are otherwise considered opaque by the watch mechanism. Bear in mind that regardless of the result or action of the watch fns the atom's value will change. Example: (def a (atom 0)) (add-watch a :inc (fn [k r o n] (assert (== 0 n)))) (swap! a inc) ;; Assertion Error (deref a) ;=> 1

No examples yet. Be the first to add one!