Skip to content

Instantly share code, notes, and snippets.

@mpenet
Forked from ztellman/foami.clj
Last active August 29, 2015 14:07
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 mpenet/6ff8559ed32d9a35c3b1 to your computer and use it in GitHub Desktop.
Save mpenet/6ff8559ed32d9a35c3b1 to your computer and use it in GitHub Desktop.
(ns foami.core
"FOreign Asynchronous Mechanism Interop"
(:require [clojure.core.async :as async]))
(defn put!
"Takes a `ch`, a `msg`, a single arg function that when passed `true` enables backpressure
and when passed `false` disables it, and a no-arg function which, when invoked, closes the
upstream source."
[ch msg backpressure! close!]
(let [status (atom :sending)]
(async/put! ch msg
(fn [result]
(if-not result
(close!)
(when (compare-and-set! status :paused :sent)
(backpressure! false)))))
(when (compare-and-set! status :sending :paused)
(backpressure! true))
nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment