Public Vars

Back

agent (clj)

(source)

function

(agent state & options)
Creates and returns an agent with an initial value of state and zero or more options (in any order): :meta metadata-map :validator validate-fn :error-handler handler-fn :error-mode mode-keyword If metadata-map is supplied, it will become the metadata on the agent. validate-fn must be nil or a side-effect-free fn of one argument, which will be passed the intended new state on any state change. If the new state is unacceptable, the validate-fn should return false or throw an exception. handler-fn is called if an action throws an exception or if validate-fn rejects a new state -- see set-error-handler! for details. The mode-keyword may be either :continue (the default if an error-handler is given) or :fail (the default if no error-handler is given) -- see set-error-mode! for details.

Examples

nextjournal/clerk
(ns viewers.controls
  "Demo of Clerk's two-way bindings."
  {:nextjournal.clerk/visibility {:code :show :result :show}}
  (:require [clojure.core :as core]
            [nextjournal.clerk :as clerk]
            [nextjournal.clerk.viewer :as viewer]))

;; We `defonce` an atom and tag it with `^::clerk/sync`. This will create a corresponding (reagent) atom in the browser.
^{::clerk/sync true}
(defonce number-atom
  (atom 0))
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))))

#?(:clj (t/defalias
          ^{:doc "Supertype of all agents."
            :forms '[AnyAgent]}
          t/AnyAgent
          (t/Instance clojure.lang.Agent)))

#?(:clj
   (t/defalias
     ^{:doc "An agent that can read and write type x."
       :forms '[(Agent x)]}
     t/Agent
     (t/TFn [[x :variance :invariant]] 
            (clojure.lang.Agent x))))

#?(:clj
   (t/defalias
     ^{:doc "An agent that can read and write type x.
            
            Deprecated: Please use t/Agent."
       :deprecated "1.1.6"
       :superseded-by `t/Agent
       :forms '[(Agent1 x)]}
     t/Agent1
     t/Agent))

cc/*1 t/Any
cc/*2 t/Any
cc/*3 t/Any
cc/*e #?(:cljs t/Any :default (t/U nil Throwable))
#?@(:cljs [] :default [
cc/*agent* (t/U nil (t/Deref t/Any) #_(t/Agent t/Any))
cc/*allow-unresolved-vars* t/Any
cc/*data-readers* (t/Map t/Sym t/AnyVar)
cc/*default-data-reader-fn* (t/U nil [t/Any t/Any :-> t/Any])
cc/*fn-loader* t/Any
cc/*math-context* t/Any
cc/*source-path* t/Str
cc/*use-context-classloader* t/Any
])
cc/*assert* t/Any

#?@(:cljs [] :default [
cc/get-thread-bindings [:-> (t/Map t/AnyVar t/Any)]
cc/bound-fn*
    (t/All [r b :..]
         [[b :.. b :-> r] :-> [b :.. b :-> r]])
cc/find-var
    [t/Sym :-> (t/Nilable t/AnyVar)]
cc/agent
    (t/All [x] [x & :optional {:validator (t/Nilable [x :-> t/Any]) :meta t/Any
                               :error-handler (t/Nilable [(t/Agent x) Throwable :-> t/Any])
                               :error-mode (t/U ':continue ':fail)} 
                :-> (t/Agent x)])
cc/set-agent-send-executor! [java.util.concurrent.ExecutorService :-> t/Any]
cc/set-agent-send-off-executor! [java.util.concurrent.ExecutorService :-> t/Any]
cc/send-via (t/All [x b :..] [(t/Agent x) [x b :.. b :-> x] b :.. b :-> (t/Agent x)])
cc/send (t/All [x b :..] [(t/Agent x) [x b :.. b :-> x] b :.. b :-> (t/Agent x)])
cc/send-off (t/All [x b :..] [(t/Agent x) [x b :.. b :-> x] b :.. b :-> (t/Agent x)])
cc/await [t/AnyAgent :* :-> nil]
cc/await-for [t/AnyInteger t/AnyAgent :* :-> t/Bool]
cc/await1 (t/All [[a :< t/AnyAgent]] [a :-> (t/Nilable a)])
cc/release-pending-sends [:-> t/AnyInteger]
])

#?@(:cljs [] :default [
cc/agent-error [t/AnyAgent :-> (t/Nilable Throwable)]
cc/restart-agent (t/All [x] [(t/Agent x) x & :optional {:clear-actions t/Any} :-> t/Any])
cc/set-error-handler! (t/All [x] [(t/Agent x) [(t/Agent x) Throwable :-> t/Any] :-> t/Any])
cc/error-handler (t/All [[a :< t/AnyAgent]] [a :-> (t/Nilable [a Throwable :-> t/Any])])
cc/set-error-mode! [t/AnyAgent (t/U ':fail ':continue) :-> t/Any]
cc/error-mode [t/AnyAgent :-> t/Any]
cc/agent-errors [t/AnyAgent :-> (t/Nilable (t/ASeq Throwable))]
cc/clear-agent-errors [t/AnyAgent :-> t/Any]
cc/shutdown-agents [:-> t/Any]
])