Back
promise (clj)
(source)function
(promise)
Returns a promise object that can be read with deref/@, and set,
once only, with deliver. Calls to deref/@ prior to delivery will
block, unless the variant of deref with timeout is used. All
subsequent derefs will return the same delivered value without
blocking. See also - realized?.
Examples
replikativ/datahike
(ns datahike.http.writer
"Remote writer implementation for datahike.http.server through datahike.http.client."
(:require [datahike.writer :refer [PWriter create-writer create-database delete-database]]
[datahike.http.client :refer [request-json] :as client]
[datahike.json :as json]
[datahike.tools :as dt :refer [throwable-promise]]
[taoensso.timbre :as log]
[clojure.core.async :refer [promise-chan put!]]))
(defrecord DatahikeServerWriter [remote-peer conn]
PWriter
(-dispatch! [_ arg-map]
(let [{:keys [op args]} arg-map
p (promise-chan)
config (:config @(:wrapped-atom conn))]
(log/debug "Sending operation to datahike-server:" op)
(log/trace "Arguments:" arg-map)
(put! p
(try
(request-json :post
(str op "-writer")
remote-peer
(vec (concat [config] args))
json/mapper)
(catch Exception e
e)))
p))
(-shutdown [_])
(-streaming? [_] false))
(defmethod create-database :datahike-server
[& args]
(let [p (throwable-promise)
{:keys [writer] :as config} (first args)]
;; redirect call to remote-peer as writer config
(deliver p (try (->
(request-json :post
"create-database-writer"
writer
(vec (concat [(-> config
(assoc :remote-peer writer)
(dissoc :writer))]
(rest args))))
(dissoc :remote-peer))
(catch Exception e
e)))
p))
(defmethod delete-database :datahike-server
[& args]
(let [p (throwable-promise)
{:keys [writer] :as config} (first args)]
;; redirect call to remote-peer as writer config
(deliver p (try
(-> (request-json :post
"delete-database-writer"
writer
(vec (concat [(-> config
(assoc :remote-peer writer)
(dissoc :writer))]
(rest args))))
(dissoc :remote-peer))
(catch Exception e
e)))
p))
clojure/core.typed
(ns ^:skip-wiki clojure.core.typed.ann.clojure
"Type annotations for the base Clojure distribution."
(:require [#?(:clj clojure.core.typed
:cljs cljs.core.typed)
:refer [defalias] :as t]))
(defalias
^{:doc "A Clojure promise (see clojure.core/{promise,deliver})."
:forms '[(Promise t)]}
t/Promise
(t/TFn [[x :variance :invariant]]
(t/Rec [p]
(t/I (t/Deref x)
(clojure.lang.IBlockingDeref x)
clojure.lang.IPending
[x -> (t/U nil p)]))))
cognitect-labs/aws-api
(ns dynamodb-examples
(:require [clojure.core.async :as a]
[clojure.java.io :as io]
[clojure.data.json :as json]
[cognitect.aws.client.api :as aws]))
(->> ["Forum" "Reply" "Thread"]
(map #(aws/invoke ddb {:op :DescribeTable
:request {:TableName %}
:ch (a/promise-chan (comp
(map :Table)
(map :TableStatus)))}))
(into #{}))
puppetlabs/trapperkeeper
(ns puppetlabs.trapperkeeper.app
(:require [schema.core :as schema]
[puppetlabs.trapperkeeper.services :as s]
[clojure.core.async.impl.protocols :as async-prot])
(:import (clojure.lang IDeref)))
(def TrapperkeeperAppContext
"Schema for a Trapperkeeper application's internal context. NOTE: this schema
is intended for internal use by TK and may be subject to minor changes in future
releases."
{:service-contexts {schema/Keyword {schema/Any schema/Any}}
:ordered-services TrapperkeeperAppOrderedServices
:services-by-id {schema/Keyword (schema/protocol s/Service)}
:lifecycle-channel (schema/protocol async-prot/Channel)
:shutdown-channel (schema/protocol async-prot/Channel)
:lifecycle-worker (schema/protocol async-prot/Channel)
:shutdown-reason-promise IDeref})
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 "A Clojure promise (see clojure.core/{promise,deliver})."
:forms '[(Promise x)]}
t/Promise
(t/TFn [[x :variance :invariant]]
(t/I (t/Deref x)
(clojure.lang.IBlockingDeref x)
clojure.lang.IPending
;; FIXME I think this might be an implementation detail.
[x :-> (t/Nilable (t/Promise x))]))))
#?@(:cljs [] :default [
cc/promise (t/All [x] [:-> (t/Promise x)])
cc/deliver (t/All [x] [(t/Promise x) x :-> (t/Nilable (t/Promise x))])
])