Fixup traces not being enabled on intial page load

This was relying on side-effects from rendering which no longer ran
in the re-frame world.
This commit is contained in:
Daniel Compton 2017-11-10 15:13:33 +13:00
parent 18331689e6
commit de167f208c
6 changed files with 60 additions and 50 deletions

View File

@ -418,7 +418,7 @@
}
#--re-frame-trace-- .filter-control-input {
display: flex;
flex: 1 0 auto;
flex: 0 0 auto;
}
#--re-frame-trace-- .nav {
background: #efeef1;

View File

@ -503,7 +503,7 @@
.filter-control-input {
display: flex;
flex: 1 0 auto;
flex: 0 0 auto;
}
.nav {
background: @light-gray;

View File

@ -120,37 +120,6 @@
(reagent.impl.batching/do-after-render (fn [] (trace/with-trace {:op-type :raf-end})))
(real-schedule)))))
(defonce total-traces (interop/ratom 0))
;(defonce traces (interop/ratom []))
(defn log-trace? [trace]
(let [render-operation? (= (:op-type trace) :render)
component-path (get-in trace [:tags :component-path] "")]
(if-not render-operation?
true
(not (str/includes? component-path "devtools outer")))
#_(if-let [comp-p (get-in trace [:tags :component-path])]
(println comp-p))))
(defn disable-tracing! []
(re-frame.trace/remove-trace-cb ::cb))
(defn enable-tracing! []
(re-frame.trace/register-trace-cb ::cb (fn [new-traces]
(when-let [new-traces (filter log-trace? new-traces)]
(swap! total-traces + (count new-traces))
(swap! utils.traces/traces
(fn [existing]
(let [new (reduce conj existing new-traces)
size (count new)]
(if (< 4000 size)
(let [new2 (subvec new (- size 2000))]
(if (< @total-traces 20000) ;; Create a new vector to avoid structurally sharing all traces forever
(do (reset! total-traces 0)
(into [] new2))))
new))))))))
(defn init-tracing!
"Sets up any initial state that needs to be there for tracing. Does not enable tracing."
@ -164,12 +133,6 @@
(def ease-transition "left 0.2s ease-out, top 0.2s ease-out, width 0.2s ease-out, height 0.2s ease-out")
(defn toggle-traces [showing?]
(if @showing?
(enable-tracing!)
(disable-tracing!)))
(defn devtools-outer [traces opts]
;; Add clear button
;; Filter out different trace types
@ -191,8 +154,7 @@
(when (and (not entering-input?) combo-key?)
(cond
(and (= key "h") (.-ctrlKey e))
(do (rf/dispatch [:settings/toggle-panel])
(toggle-traces showing?)
(do (rf/dispatch [:settings/user-toggle-panel])
(.preventDefault e))))))
handle-mousemove (fn [e]
(when @dragging?
@ -204,8 +166,7 @@
(reset! window-width new-window-width))))
handle-mouse-up (fn [e] (reset! dragging? false))]
(r/create-class
{:component-will-mount (fn []
(toggle-traces showing?)
{:component-did-mount (fn []
(js/window.addEventListener "keydown" handle-keys)
(js/window.addEventListener "mousemove" handle-mousemove)
(js/window.addEventListener "mouseup" handle-mouse-up)

View File

@ -12,4 +12,6 @@
(rf/dispatch [:settings/show-panel? show-panel?])
(rf/dispatch [:settings/selected-tab selected-tab])
(rf/dispatch [:traces/filter-items filter-items])
(rf/dispatch [:app-db/paths app-db-paths])))
(rf/dispatch [:app-db/paths app-db-paths])
(when show-panel?
(rf/dispatch [:global/enable-tracing]))))

View File

@ -27,9 +27,14 @@
(assoc-in db [:settings :show-panel?] show-panel?)))
(rf/reg-event-db
:settings/toggle-panel
:settings/user-toggle-panel
(fn [db _]
(let [show-panel? (not (get-in db [:settings :show-panel?]))]
(let [show-panel? (not (get-in db [:settings :show-panel?]))
external-panel? (get-in db [:settings :external-window?])]
(if show-panel?
(utils.traces/enable-tracing!)
(when-not external-panel?
(utils.traces/disable-tracing!)))
(localstorage/save! "show-panel" show-panel?)
(assoc-in db [:settings :show-panel?] show-panel?))))
@ -62,16 +67,29 @@
(rf/reg-event-fx
:global/launch-external
(fn [{:keys [db]} _]
(fn [ctx _]
(open-debugger-window)
{:db db
{:db (assoc-in (:db ctx) [:settings :external-window?] true)
;; TODO: capture the intent that the user is still interacting with devtools, to persist between reloads.
:dispatch-later [{:ms 200 :dispatch [:settings/show-panel? false]}]}))
(rf/reg-event-fx
:global/external-closed
(fn [ctx _]
{:dispatch-later [{:ms 400 :dispatch [:settings/show-panel? true]}]}))
{:db (assoc-in (:db ctx) [:settings :external-window?] false)
:dispatch-later [{:ms 400 :dispatch [:settings/show-panel? true]}]}))
(rf/reg-event-fx
:global/enable-tracing
(fn [ctx _]
(utils.traces/enable-tracing!)
ctx))
(rf/reg-event-fx
:global/disable-tracing
(fn [ctx _]
(utils.traces/disable-tracing!)
ctx))
;; Traces

View File

@ -1,5 +1,34 @@
(ns day8.re-frame.trace.utils.traces
(:require [reagent.core :as r]))
(:require [reagent.core :as r]
[clojure.string :as str]))
;; Put here to avoid cyclic dependencies
(defonce traces (r/atom []))
(defonce total-traces (r/atom 0))
(defn log-trace? [trace]
(let [render-operation? (= (:op-type trace) :render)
component-path (get-in trace [:tags :component-path] "")]
(if-not render-operation?
true
(not (str/includes? component-path "devtools outer")))))
(defn disable-tracing! []
(println "Disabling tracing")
(re-frame.trace/remove-trace-cb ::cb))
(defn enable-tracing! []
(println "Enabling tracing")
(re-frame.trace/register-trace-cb ::cb (fn [new-traces]
(when-let [new-traces (filter log-trace? new-traces)]
(swap! total-traces + (count new-traces))
(swap! traces
(fn [existing]
(let [new (reduce conj existing new-traces)
size (count new)]
(if (< 4000 size)
(let [new2 (subvec new (- size 2000))]
(if (< @total-traces 20000) ;; Create a new vector to avoid structurally sharing all traces forever
(do (reset! total-traces 0)
(into [] new2))))
new))))))))