Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
37 lines (30 sloc) 1.1 KB
(def listeners (atom {}))
(def ^:dynamic *current-event* nil)
(defn- ->coll [val]
(if (coll? val) val [val]))
(defn register [event callback & curried]
(let [binding {:callback callback
:curried curried}]
(swap! listeners update-in [(->coll event)]
conj binding)
(defn unregister [event binding]
(swap! listeners update-in [(->coll event)]
(fn [bindings]
(remove #(identical? binding %) bindings))))
(defn register-once [event callback & curried]
(let [binding (atom nil)
new-callback (fn [& args]
(unregister event @binding)
(apply callback args))]
(reset! binding (apply register event new-callback curried))
(defn fire [event & args]
"event is either a single value or a sequence."
(binding [*current-event* event]
(let [events (->coll event)]
(doseq [event (reductions conj [] events)]
(doseq [{callback :callback
curried :curried} (get-in @listeners [event])]
(apply callback (concat curried args)))))))
Jump to Line
Something went wrong with that request. Please try again.