Skip to content

Commit

Permalink
added a dispatch-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
stumitchell committed Jan 5, 2015
1 parent d733c9c commit f442460
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/re_frame/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f442460

Please sign in to comment.