src/clojurecraft/events.clj @ 771c6a626c93

Be less lazy!
author Steve Losh <steve@stevelosh.com>
date Fri, 12 Aug 2011 19:01:19 -0400
parents dfb084418ef9
children 82b346fcfbf2
(ns clojurecraft.events)

(defn add-handler [bot event-type handler]
  (dosync
    (let [current-handlers (event-type @(:event-handlers bot))
          updated-handlers (conj current-handlers handler)]
      (alter (:event-handlers bot) assoc event-type updated-handlers))))

(defn clear-handlers [bot event-type]
  (dosync (alter (:event-handlers bot) dissoc event-type)))


(defn- fire-handler [bot event-type & args]
  (let [action-groups (map #(apply (deref %) (into [bot] args))
                           (event-type @(:event-handlers bot)))
        queue-action #(.put (:actionqueue bot) %)
        queue-action-group #(dorun (map queue-action %))]
    (dorun (map queue-action-group action-groups))))

(defn fire-chat [bot message]
  (fire-handler bot :chat message))