Event queue functionality

This commit is contained in:
Jeremy Penner 2013-03-07 23:18:57 -05:00
parent 6b56991689
commit 1118b012f1

34
src/hottub/event.clj Normal file
View file

@ -0,0 +1,34 @@
(ns hottub.event)
(defn new-evq [] (ref clojure.lang.PersistentQueue/EMPTY))
(def ^:dynamic *evq* nil)
(defmacro with-evq [evqueue & body]
`(binding [*evq* ~evqueue] (dosync ~@body)))
(defn post-event-to [evq event]
(dosync (ref-set evq (conj @evq event))))
(defn post-event [event]
(post-event-to *evq* event))
(defn handle-next-event [evq & dispatchersInit]
(dosync (let [queueold @evq]
(if (seq queueold)
(let [queuenew (pop queueold) event (peek queueold)]
(loop [dispatchers dispatchersInit]
(if (seq dispatchers)
(do
((first dispatchers) event)
(recur (next dispatchers)))
(do
(ref-set evq queuenew)
true))))
false))))
(defn handle-events [evq & dispatchers]
(loop [more-events true]
(if more-events (recur (apply handle-next-event evq dispatchers)))))
(defn get-evq [] *evq*)