Skip to content

Commit

Permalink
Add dynamic var for warning on handler overwrite
Browse files Browse the repository at this point in the history
When figwheel reloads code, it causes handlers to be re-registered
which leads to a warning for each handler. This leads to noisy logs.
Figwheel has a :before-jsload key which can be called before it reloads
application code.

This patch adds a *warn-on-overwrite* dynamic var, and shows how to use
it in the example project.

One side effect of using this is that you will never get a warning
for duplicate handlers when Figwheel is reloading. It may be possible
to track how many handlers were reloaded in a particular Figwheel
reload, but this would get very complex. In any case, you will still
get warnings every time you refresh the browser, which should be good
enough for the relatively rare case of creating duplicate handlers.

Fixes #204
  • Loading branch information
danielcompton committed Nov 6, 2016
1 parent 41990cf commit 8de4175
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion examples/todomvc/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions examples/todomvc/src/todomvc/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -13,15 +14,15 @@


;; -- 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 ------------------------------------------------------

(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))))
Expand All @@ -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))

3 changes: 2 additions & 1 deletion src/re_frame/registrar.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8de4175

Please sign in to comment.