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
|(:require [cljs.core.async :refer [chan close!o sliding-buffer]]|
|[clojure.string :as string])|
|[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"|
|(>! c [(.-x e) (.-y e)]))))|
|(defn timeout [ms]|
|(let [c (chan)]|
|(js/setTimeout (fn  (close! c)) ms)|
|(defn throttle [c ms]|
|(let [c' (chan)]|
|(>! c' (<! c))|
|(<! (timeout ms))))|
|(def throttled (throttle c 500))|
|(let [loc (<! throttled)]|
|(aset loc-div "innerHTML" (string/join ", " loc)))))|
Jun 28, 2013
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.
Jun 28, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
If I understand correctly,
cis an unbuffered channel and each fire of the mousemove event handler creates a new IOC thread (goroutine?) that blocks until it can put a value onto the channel---which they can only do once every 500 ms when the consuming IOC thread's loop iterates.
As far as I know, there is no guarantee about execution order of the IOC threads, so in this example there's no reason to expect that the rendered mouse coordinates are the most recent coordinates from the past 500 ms.
Is that correct?