Permalink
Browse files

added in-memory event-bus

  • Loading branch information...
1 parent 8b579ce commit 5237b397da54c3023ab089013683c3fadb6dacd5 @wilkes committed Apr 15, 2012
Showing with 34 additions and 1 deletion.
  1. +2 −1 project.clj
  2. +32 −0 src/clj/projected/event_bus.clj
View
@@ -10,7 +10,8 @@
[postgresql "9.1-901.jdbc4"]
[crate "0.1.0-alpha3"]
[jayq "0.1.0-alpha3"]
- [fetch "0.1.0-alpha2"]]
+ [fetch "0.1.0-alpha2"]
+ [lamina "0.5.0-alpha1"]]
:dev-dependencies [[speclj "2.1.1"]]
:test-path "spec/"
:plugins [[lein-cljsbuild "0.1.7"]]
@@ -0,0 +1,32 @@
+(ns projected.event-bus
+ (require [lamina.core :as lamina]))
+
+(defprotocol EventBus
+ (publish [this event])
+ (subscribe [this event-name handler]))
+
+
+;; In memory impl
+(extend-type lamina.core.channel.Channel
+ EventBus
+ (publish [ch event]
+ (lamina/enqueue ch event))
+ (subscribe [ch event-name handler]
+ (lamina/receive-all (lamina/filter* #(= (:type %) event-name)
+ ch)
+ handler)))
+
+(defonce ^:dynamic *event-bus* (atom nil))
+(defonce ^:dynamic *subscriptions* (atom #{}))
+
+(defn in-process-bus []
+ (swap! *event-bus* lamina/channel)
+ (doseq [[event-name subscriber] @*subscriptions*]
+ (subscribe @*event-bus* event-name subscriber)))
+
+
+(defn add-subscriber [event-name subscriber]
+ (swap! *subscriptions* conj [event-name subscriber]))
+
+(defn publish-event [event]
+ (publish @*event-bus* event))

0 comments on commit 5237b39

Please sign in to comment.