Last active
August 29, 2015 14:12
-
-
Save mpenet/709c472aec3e69bfbdb3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defn put! | |
"Takes a `ch`, a `msg`, a fn that enables backpressure, one that disables it, | |
and a no-arg function which, when invoked, closes the upstream source." | |
([ch msg suspend! resume! close!] | |
(let [status (atom ::sending)] | |
(async/put! ch msg | |
(fn [result] | |
(if-not result | |
(when close! (close!)) | |
(do | |
(when (identical? @status ::paused) | |
;; if it was paused resume | |
(resume!)) | |
(reset! status ::sent))))) | |
;; it's still sending, means it's parked, so suspend source | |
(when (compare-and-set! status ::sending ::paused) | |
(suspend!)) | |
nil)) | |
([ch msg suspend! resume!] | |
(put! ch msg suspend! resume! nil))) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
but know that I think of it more, I prefer your arguments style ... it's more anonymous to the side effect.