diff --git a/examples/todomvc/project.clj b/examples/todomvc/project.clj index d4af764..6f19111 100644 --- a/examples/todomvc/project.clj +++ b/examples/todomvc/project.clj @@ -17,7 +17,8 @@ :source-map true :source-map-timestamp true :main "todomvc.core"} - :figwheel {:on-jsload "todomvc.core/main"}}}}} + :figwheel {:before-jsload "todomvc.core/before_reload" + :on-jsload "todomvc.core/figwheel_reload"}}}}} :prod {:cljsbuild {:builds {:client {:compiler {:optimizations :advanced diff --git a/examples/todomvc/src/todomvc/core.cljs b/examples/todomvc/src/todomvc/core.cljs index effcf37..8bb6135 100644 --- a/examples/todomvc/src/todomvc/core.cljs +++ b/examples/todomvc/src/todomvc/core.cljs @@ -3,6 +3,7 @@ (:require [goog.events :as events] [reagent.core :as reagent] [re-frame.core :refer [dispatch dispatch-sync]] + [re-frame.registrar :as registrar] [secretary.core :as secretary] [todomvc.events] [todomvc.subs] @@ -13,7 +14,7 @@ ;; -- Debugging aids ---------------------------------------------------------- -(devtools/install!) ;; we love https://github.com/binaryage/cljs-devtools +(defn install-devtools [] (devtools/install!)) ;; we love https://github.com/binaryage/cljs-devtools (enable-console-print!) ;; so println writes to console.log ;; -- Routes and History ------------------------------------------------------ @@ -21,7 +22,7 @@ (defroute "/" [] (dispatch [:set-showing :all])) (defroute "/:filter" [filter] (dispatch [:set-showing (keyword filter)])) -(def history +(defn enable-history [] (doto (History.) (events/listen EventType.NAVIGATE (fn [event] (secretary/dispatch! (.-token event)))) @@ -30,9 +31,24 @@ ;; -- Entry Point ------------------------------------------------------------- -(defn ^:export main +(defn render [] - (dispatch-sync [:initialise-db]) (reagent/render [todomvc.views/todo-app] (.getElementById js/document "app"))) +(defn before-reload + "Set *warn-on-overwrite* false so that we don't get lots of warnings from Figwheel when reloading." + [] + (set! registrar/*warn-on-overwrite* false)) + +(defn figwheel-reload [] + (set! registrar/*warn-on-overwrite* true) + (render)) + +(defn ^:export main + [] + (install-devtools) + (enable-history) + (dispatch-sync [:initialise-db]) + (render)) + diff --git a/src/re_frame/registrar.cljc b/src/re_frame/registrar.cljc index 415239c..91970bc 100644 --- a/src/re_frame/registrar.cljc +++ b/src/re_frame/registrar.cljc @@ -8,6 +8,7 @@ ;; kinds of handlers (def kinds #{:event :fx :cofx :sub}) +(def ^:dynamic *warn-on-overwrite* true) ;; This atom contains a register of all handlers. ;; Contains a map keyed first by `kind` (of handler), and then `id`. @@ -35,7 +36,7 @@ (defn register-handler [kind id handler-fn] (when debug-enabled? ;; This is in a separate when so Closure DCE can run - (when (get-handler kind id false) + (when (and (get-handler kind id false) *warn-on-overwrite*) (console :warn "re-frame: overwriting" (str kind) "handler for:" id))) ;; allow it, but warn. Happens on figwheel reloads. (swap! kind->id->handler assoc-in [kind id] handler-fn) handler-fn) ;; note: returns the just registered handler