Skip to content

Instantly share code, notes, and snippets.

@halgari
Created August 16, 2013 17:16
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 halgari/6251727 to your computer and use it in GitHub Desktop.
Save halgari/6251727 to your computer and use it in GitHub Desktop.
(defn throttle-chan [input]
(let [throttle-chan (chan)
output-chan (chan)]
(go
(loop [throttle-msec nil]
(let [t-chan (when (= :disabled throttle-msec)
(timeout throttle-msec))
[val c] (alt! [input throttle-chan])]
(if (= c throttle-chan)
(recur val)
(do
(>! output-chan val)
(when t-chan
(<! t-chan))
(recur throttle-msec))))))
[output-chan throttle-chan]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment