Last active
July 4, 2021 13:52
-
-
Save swannodette/5886048 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns async-test.throttle.core | |
(:require [cljs.core.async :refer [chan close!o sliding-buffer]] | |
[clojure.string :as string]) | |
(:require-macros | |
[cljs.core.async.macros :as m :refer [go alts!]])) | |
(def c (chan (sliding-buffer 1))) | |
(def loc-div (.getElementById js/document "location")) | |
(.addEventListener js/window "mousemove" | |
(fn [e] | |
(go | |
(>! c [(.-x e) (.-y e)])))) | |
(defn timeout [ms] | |
(let [c (chan)] | |
(js/setTimeout (fn [] (close! c)) ms) | |
c)) | |
(defn throttle [c ms] | |
(let [c' (chan)] | |
(go | |
(while true | |
(>! c' (<! c)) | |
(<! (timeout ms)))) | |
c')) | |
(def throttled (throttle c 500)) | |
(go | |
(while true | |
(let [loc (<! throttled)] | |
(aset loc-div "innerHTML" (string/join ", " loc))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code produces javascript errors when I move the mouse (Chrome).
It works when switching c to
(def c (chan (async/sliding-buffer 1)))
. I guess "pausing" doesn't quite work yet (in the CLJS implementation) otherwise(>! c [x y])
would "pause" until the event is consumed, which only happens every 500ms.