Skip to content

Instantly share code, notes, and snippets.

@jjttjj
Forked from hiredman/scratch.clj
Created January 6, 2021 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjttjj/b7621c01fb3692d76652440c98b9711a to your computer and use it in GitHub Desktop.
Save jjttjj/b7621c01fb3692d76652440c98b9711a to your computer and use it in GitHub Desktop.
(require '[clojure.core.async.impl.protocols :as impl]
'[clojure.core.async.impl.dispatch :as dispatch])
;;=> nil
(extend-type java.util.concurrent.CompletionStage
impl/ReadPort
(take! [this handler]
(.whenCompleteAsync this
(reify
java.util.function.BiConsumer
(accept [_ result exception]
(.lock handler)
(let [good (and (impl/active? handler)
(impl/commit handler))]
(.unlock handler)
(when good
(dispatch/run #(good (or exception result))))))))
nil))
;;=> nil
(require '[clojure.core.async :as async])
;;=> nil
(def cf1 (java.util.concurrent.CompletableFuture.))
;;=> #'user/cf1
(first (async/alts!! [(async/timeout 1000) cf1]))
;;=> nil
(.complete cf1 "Foo")
;;=> true
(first (async/alts!! [(async/timeout 1000) cf1]))
;;=> "Foo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment