Skip to content

Instantly share code, notes, and snippets.

@athos athos/debounce.cljs
Created Jan 22, 2018

Embed
What would you like to do?
(require '[clojure.core.async :as a]
'[kitchen-async.promise :as p]
'[kitchen-async.promise.from-channel])
(comment
;; cf. https://gist.github.com/swannodette/5888989
(defn debounce
([c ms] (debounce (a/chan) c ms))
([c' c ms]
(a/go
(loop [start nil loc (a/<! c)]
(if (nil? start)
(do
(>! c' loc)
(recur (js/Date.) nil))
(let [loc (a/<! c)]
(if (>= (- (js/Date.) start) ms)
(recur nil loc)
(recur (js/Date.) loc))))))
c'))
)
;; Using kitchen-async, this can also be written as follows:
(defn debounce
([c ms] (debounce (a/chan) c ms))
([c' c ms]
(p/loop [start nil loc c]
(if (nil? start)
(do
(a/put! c' loc)
(p/recur (js/Date.) nil))
(p/let [loc c]
(if (>= (- (js/Date.) start) ms)
(p/recur nil loc)
(p/recur (js/Date.) loc)))))
c'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.