Public Vars

Back

add-watch (clj)

(source)

function

(add-watch reference key fn)
Adds a watch function to an agent/atom/var/ref 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, on the agent's thread if an agent, before any pending sends if agent or ref. Note that an atom's or ref's state may have changed again prior to the fn call, so use old/new-state rather than derefing the reference. Note also that watch fns may be called from multiple threads simultaneously. Var watchers are triggered only by root binding changes, not thread-local set!s. 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.

Examples

typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))

cc/add-watch (t/All [x [a :< (#?(:clj IRef :cljs t/Atom) x)]]
                    (t/IFn
                      ; this arity remembers the type of reference we pass to the function
                      [a t/Any [t/Any a x x :-> t/Any] :-> t/Any]
                      ; if the above cannot be inferred, 
                      [(#?(:clj IRef :cljs t/Atom) x) t/Any [t/Any (#?(:clj IRef :cljs t/Atom) x) x x :-> t/Any] :-> t/Any]))
cc/remove-watch (t/All [x] [(#?(:clj IRef :cljs t/Atom) x) t/Any :-> t/Any])
cjbarre/multi-gpt
  ;; Load the namespaces and required libraries
  (require '[multi-gpt.conversation-manager :as cm]
           '[multi-gpt.repl-interface :refer :all]
           '[multi-gpt.task-manager :as tm]
           '[clojure.core.async :refer [<!! go]]
           '[clojure.pprint :refer [pprint]])

  (add-watch (-> system :task-manager :db)
             :on-update
             (fn [_ _ _ new-state]
               (pprint
                (get new-state (:task-id task)))))
logicblocks/salutem
(ns salutem.core.maintenance-test
  (:require
   [clojure.test :refer :all]
   [clojure.core.async :as async]

    (add-watch registry-store :watcher
      (fn [_ _ _ _]
        (reset! updated? true)))

    (add-watch registry-store :watcher
      (fn [_ _ _ _]
        (swap! updated-count inc)))
fromheten/rad
(ns rad.state
  "Contains shared state for rad."
  (:require [clojure.core.async :as a :refer [chan go >!]]))

(def current-buffer (atom ["Rad is meant"
                           "to be hacked"]))
(def buffer-updates-channel
  (let [channel (chan)]
    (add-watch current-buffer :_
               (fn [_ _ _ new-state]
                 (go (>! channel new-state))))
    channel))

(def point (atom [0 0]))
(def point-update-channel
  (let [channel (chan)]
    (add-watch point :_
               (fn [_ _ _ new-state]
                 (a/put! channel new-state)))
    channel))
tatut/re-html-template
(ns re-html-template.core-test
  (:require [re-html-template.core :refer [html-template html define-html-template]]
            [clojure.test :refer [deftest is testing]]
            [clojure.core.match :refer [match]]
            [clojure.string :as str]))

(deftest reload-test
  (spit "reload.html" "<html><body>INITIAL</body></html>")
  (let [tpl (binding [*ns* this-ns]
              (eval '(html-template
                      [x]
                      {:file "reload.html"
                       :selector "body"
                       :reload? true}
                      :body {:append-children x})))
        reloaded? (promise)]
    (add-watch re-html-template.core/reloads :reload-watcher
               (fn [& _]
                 (deliver reloaded? true)))
    (is (= [:body {} "INITIAL" 42]
           (tpl 42)))
    (spit "reload.html" "<html><body>RELOADED</body></html>")
    (is (deref reloaded? 5000 false)
        "reload didn't happen within 5 seconds")
    (is (= [:body {} "RELOADED" 666]
           (tpl 666)))))