I have need to perform an action when a series of events is quiet for some
period. That is, if one event arrives an action is queued to execute after
some timeout. If a second event arrives the timeout is reset, and so-forth.
The following code seems to work, however I'm wondering if calling 'future'
from 'swap!' is a bad idea (side effecting), and if there's a better way.
(defn queue-with-delay [period func]
(let [f (atom nil)]
(fn []
(when @f
(future-cancel @f))
(swap! f (fn [_] (future (Thread/sleep period) (func)))))))
Use like
(def event (queue-with-delay 2000 #(println "running")))
(event)
(event)
(event) ; pause 2 sec
"running"
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.