Coincidentally, we recently wrote code to do something very similar. The following function will invoke f
after period
milliseconds, unless a value is sent on events-ch
, in which case the timeout is reset (and starts counting down again):
(defn invoke-after-uninterrupted-delay
([period events-ch f]
(invoke-after-uninterrupted-delay period events-ch f []))
([period events-ch f & args]
(async/go-loop []
(let [[_ p] (async/alts! [(async/timeout period) events-ch])]
(if (= p events-ch)
(recur)
(apply f args))))))
e
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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
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+unsubscribe@googlegroups.com.
It does seem like a single-thread solution would be better, not creating so many futures. Polling seems pretty crude, but I don't see another way of doing it with clojure abstractions. Maybe a pure java solution.
Erik that's pretty! But be careful about go-loops and closed channels. This will recur infinitely if events-ch is closed (it will continuously return nil)
(defn invoke-after-uninterrupted-delay
([period events-ch f]
(invoke-after-uninterrupted-delay period events-ch f []))
([period events-ch f & args]
(async/go-loop []
(let [[v p] (async/alts! [(async/timeout period) events-ch])]
(when v
(if (= p events-ch)
(recur)
(apply f args)))))))
(defn timeout-channel"Creates a channel and a go block that takes from it. The go block keepsan internal status with two possible values, `:wait` and `:receive`.In ':wait' status, execution is blocked until there's a value available in thechannel, it then enters the ':receive' status, until the timeout wins.Returns the channel where events need to be pushed."[timeout-ms f](let [c (async/chan)](async/go-loop [status :waitargs nil](condp = status:wait(recur :receive (async/<! c)):receive(let [[_ ch] (async/alts! [c (async/timeout timeout-ms)])](if (= ch c)(recur :receive args)(do(async/thread (if (sequential? args) (apply f args) (f args)))(recur :wait nil))))))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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 a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ereNh_csKbs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.