Merge pull request #1 from stumitchell/master

merged stu's added a dispatch-sync
This commit is contained in:
hipitihop 2015-01-06 12:36:02 +10:00
commit f6ce0dcde9
1 changed files with 10 additions and 5 deletions

View File

@ -31,16 +31,21 @@
(assert (some? event-v)) ;; nil would close the channel
(put! dispatch-chan event-v))
(defn dispatch-sync
"sync version of above that actually does the dispatch"
[event-v]
(let [event-id (first-in-vector event-v)
handler-fn (get @id->fn event-id)]
(assert (not (nil? handler-fn)) (str "No event handler registered for event: " event-id ))
(handler-fn app-db event-v)))
(defn- router
"route an event, arriving on the dispatch channel, to the right handler"
[]
(go-loop []
(let [event-v (<! dispatch-chan)
event-id (first-in-vector event-v)
handler-fn (get @id->fn event-id)]
(assert (not (nil? handler-fn)) (str "No event handler registered for event: " event-id ))
(handler-fn app-db event-v)
(let [event-v (<! dispatch-chan)]
(dispatch-sync event-v)
(recur))))
(router) ;; run the router loop it