Skip to content

Instantly share code, notes, and snippets.

@halgari
Last active December 21, 2015 04:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halgari/6251731 to your computer and use it in GitHub Desktop.
Save halgari/6251731 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-not (= :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]))
@ericnormand
Copy link

A way to toggle

(defn toggled-fan-out [source]
  (let [a (chan)
         b (chan)
         t (chan)]
    (go
      (loop [ts (cycle [a b])]
         (let [current (first ts)
                [v c] (alts! [t source])]
            (if (= c t) ;; got a toggle message
               (recur (rest ts))
               (do
                 (>! current v)
                 (recur ts)))))
    {:a a
     :b b
     :toggle t})))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment