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 (assert (some? event-v)) ;; nil would close the channel
(put! dispatch-chan event-v)) (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 (defn- router
"route an event, arriving on the dispatch channel, to the right handler" "route an event, arriving on the dispatch channel, to the right handler"
[] []
(go-loop [] (go-loop []
(let [event-v (<! dispatch-chan) (let [event-v (<! dispatch-chan)]
event-id (first-in-vector event-v) (dispatch-sync 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)
(recur)))) (recur))))
(router) ;; run the router loop it (router) ;; run the router loop it