Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created February 6, 2020 00:27
Show Gist options
  • Save hiredman/1788aa052f26d127c00a1679656026f0 to your computer and use it in GitHub Desktop.
Save hiredman/1788aa052f26d127c00a1679656026f0 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