From 1118b012f1727986fe0b3e6532a1cc19ce48136c Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Thu, 7 Mar 2013 23:18:57 -0500 Subject: [PATCH] Event queue functionality --- src/hottub/event.clj | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/hottub/event.clj diff --git a/src/hottub/event.clj b/src/hottub/event.clj new file mode 100644 index 0000000..ee0cec4 --- /dev/null +++ b/src/hottub/event.clj @@ -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*) \ No newline at end of file