Add dynamic var for warning on handler overwrite

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
This commit is contained in:
Daniel Compton 2016-10-17 14:36:32 +13:00
parent 41990cf60f
commit 8de4175c6e
No known key found for this signature in database
GPG Key ID: A6E4DC5283B5DCEC
3 changed files with 24 additions and 6 deletions

View File

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

View File

@ -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))

View File

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