Skip to content

Instantly share code, notes, and snippets.

@wilkerlucio
Forked from scttnlsn/debounce.cljs
Created June 20, 2016 13:12
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 wilkerlucio/b5240b915edc0ae1894f6e0f5a850e4d to your computer and use it in GitHub Desktop.
Save wilkerlucio/b5240b915edc0ae1894f6e0f5a850e4d to your computer and use it in GitHub Desktop.
core.async debounce
(defn debounce [in ms]
(let [out (chan)]
(go-loop [last-val nil]
(let [val (if (nil? last-val) (<! in) last-val)
timer (timeout ms)
[new-val ch] (alts! [in timer])]
(condp = ch
timer (do (>! out val) (recur nil))
in (if new-val (recur new-val)))))
out))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment