(add obj f)(add obj k f)(add obj k f opts)Adds a watch function through the IWatch protocol
(def subject (atom nil)) (def observer (atom nil))
(watch/add subject :follow (fn [_ _ _ n] (reset! observer n))) (reset! subject 1) @observer => 1
;; options can be given to either transform ;; the current input as well as to only execute ;; the callback if there is a difference.
(def subject (atom {:a 1 :b 2})) (def observer (atom nil))
(watch/add subject :clone (fn [_ _ p n] (reset! observer n)) {:select :b :diff true})
(swap! subject assoc :a 0) ;; change in :a does not @observer => nil ;; affect watch
(swap! subject assoc :b 1) ;; change in :b does @observer => 1
Adds a watch function through the IWatch protocol
(def subject (atom nil))
(def observer (atom nil))
(watch/add subject :follow
(fn [_ _ _ n]
(reset! observer n)))
(reset! subject 1)
@observer => 1
;; options can be given to either transform
;; the current input as well as to only execute
;; the callback if there is a difference.
(def subject (atom {:a 1 :b 2}))
(def observer (atom nil))
(watch/add subject :clone
(fn [_ _ p n] (reset! observer n))
{:select :b
:diff true})
(swap! subject assoc :a 0) ;; change in :a does not
@observer => nil ;; affect watch
(swap! subject assoc :b 1) ;; change in :b does
@observer => 1(clear obj)(clear obj opts)Clears all watches form the object
(def subject (atom nil)) (do (watch/add subject :a (fn [_ _ _ n])) (watch/add subject :b (fn [_ _ _ n])) (watch/clear subject) (watch/list subject)) => {}
Clears all watches form the object
(def subject (atom nil))
(do (watch/add subject :a (fn [_ _ _ n]))
(watch/add subject :b (fn [_ _ _ n]))
(watch/clear subject)
(watch/list subject))
=> {}(copy to from)(copy to from opts)Copies watches from one object to another (def obj-a (atom nil)) (def obj-b (atom nil)) (do (watch/set obj-a {:a (fn [_ _ _ n]) :b (fn [_ _ _ n])}) (watch/copy obj-b obj-a) (watch/list obj-b)) => (contains {:a fn? :b fn?})
Copies watches from one object to another
(def obj-a (atom nil))
(def obj-b (atom nil))
(do (watch/set obj-a {:a (fn [_ _ _ n])
:b (fn [_ _ _ n])})
(watch/copy obj-b obj-a)
(watch/list obj-b))
=> (contains {:a fn? :b fn?})(list obj)(list obj opts)Lists watch functions through the IWatch protocol
(def subject (atom nil)) (do (watch/add subject :a (fn [_ _ _ n])) (watch/add subject :b (fn [_ _ _ n])) (watch/list subject)) => (contains {:a fn? :b fn?})
Lists watch functions through the IWatch protocol
(def subject (atom nil))
(do (watch/add subject :a (fn [_ _ _ n]))
(watch/add subject :b (fn [_ _ _ n]))
(watch/list subject))
=> (contains {:a fn? :b fn?})(process-options opts f)helper function for building a watch function
(watch/process-options {:supress true :diff true :mode :async :args 2} (fn [_ _] 1))
helper function for building a watch function
(watch/process-options {:supress true
:diff true
:mode :async
:args 2}
(fn [_ _] 1))(remove obj)(remove obj k)(remove obj k opts)Removes watch function through the IWatch protocol
(def subject (atom nil)) (do (watch/add subject :a (fn [_ _ _ n])) (watch/add subject :b (fn [_ _ _ n])) (watch/remove subject :b) (watch/list subject)) => (contains {:a fn?})
Removes watch function through the IWatch protocol
(def subject (atom nil))
(do (watch/add subject :a (fn [_ _ _ n]))
(watch/add subject :b (fn [_ _ _ n]))
(watch/remove subject :b)
(watch/list subject))
=> (contains {:a fn?})(set obj watches)(set obj watches opts)Sets a watch in the form of a map (def obj (atom nil)) (do (watch/set obj {:a (fn [_ _ _ n]) :b (fn [_ _ _ n])}) (watch/list obj)) => (contains {:a fn? :b fn?})
Sets a watch in the form of a map
(def obj (atom nil))
(do (watch/set obj {:a (fn [_ _ _ n])
:b (fn [_ _ _ n])})
(watch/list obj))
=> (contains {:a fn? :b fn?})(wrap-diff f)only functions when the inputs are different
((watch/wrap-diff (fn [_ _ p n] (+ p n))) nil nil 2 2) => 2
((watch/wrap-diff (fn [_ _ p n] (+ p n))) nil nil 2 3) => 5
only functions when the inputs are different
((watch/wrap-diff (fn [_ _ p n]
(+ p n)))
nil nil 2 2)
=> 2
((watch/wrap-diff (fn [_ _ p n]
(+ p n)))
nil nil 2 3)
=> 5(wrap-mode f mode)changes how the function is run, :sync (same thread) and :async (new thread)
changes how the function is run, :sync (same thread) and :async (new thread)
(wrap-select f sel)enables operating on a given key
((watch/wrap-select (fn [_ _ p n] (+ p n)) :a) nil nil {:a 2} {:a 1}) => 3
enables operating on a given key
((watch/wrap-select (fn [_ _ p n]
(+ p n))
:a)
nil nil {:a 2} {:a 1})
=> 3(wrap-suppress f)runs the function but if errors, does not throw exception
runs the function but if errors, does not throw exception
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |