Liking cljdoc? Tell your friends :D

hara.watch


addclj

(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
raw docstring

clearclj

(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))
=> {}
raw docstring

copyclj

(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?})
raw docstring

listclj

(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?})
raw docstring

process-optionsclj

(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))
raw docstring

removeclj

(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?})
raw docstring

setclj

(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?})
raw docstring

wrap-diffclj

(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
raw docstring

wrap-modeclj

(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)
raw docstring

wrap-selectclj

(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
raw docstring

wrap-suppressclj

(wrap-suppress f)

runs the function but if errors, does not throw exception

runs the function but if errors, does not throw exception
raw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close