(ns re-frame.handlers (:refer-clojure :exclude [flush]) (:require-macros [cljs.core.async.macros :refer [go-loop go]]) (:require [reagent.core :refer [flush]] [re-frame.db :refer [app-db]] [re-frame.utils :refer [first-in-vector warn]] [cljs.core.async :refer [chan put! fn (atom {})) (defn register "register a handler for an event" [event-id handler-fn] (when (contains? @id->fn event-id) (warn "re-frame: overwriting an event-handler" event-id)) ;; allow it, but warn. (swap! id->fn assoc event-id handler-fn)) ;; -- The Event Conveyor Belt -------------------------------------------------------------------- ;; ;; Moves events from "dispatch" to the router loop. ;; Key architecutal purpose is to cause aysnc handling of events. (def ^:private event-chan (chan)) ;; TODO: how big should we make the buffer? ;; -- lookup and call ----------------------------------------------------------------------------- (defn- handle "Look up the handler for the given event, then call it, passing in 2 parameters." [event-v] (let [event-id (first-in-vector event-v) handler-fn (get @id->fn event-id)] (if (nil? handler-fn) (warn "re-frame: no event handler registered for: \"" event-id "\". Ignoring.") ;; TODO: make exception (handler-fn app-db event-v)))) ;; -- router loop --------------------------------------------------------------------------------- ;; ;; In a loop, read events from the dispatch channel, and route them ;; to the right handler. ;; ;; Because handlers occupy the CPU, before each event is handled, hand ;; back control to the browser, via a ( @db (assoc :undo-description description) mutation-fn validation-fn))))