Skip to content

Instantly share code, notes, and snippets.

@athos
Created January 22, 2018 13:36
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 athos/e3d84503c15cc4080ef8a25d180d9664 to your computer and use it in GitHub Desktop.
Save athos/e3d84503c15cc4080ef8a25d180d9664 to your computer and use it in GitHub Desktop.
(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