Created
February 6, 2020 00:27
-
-
Save hiredman/1788aa052f26d127c00a1679656026f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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