Rework to use re-frame events rather than reagent

This commit is contained in:
Daniel Compton 2017-11-06 15:34:32 +13:00
parent cb868cb5ac
commit 5f6ff4fe9d
6 changed files with 88 additions and 36 deletions

View File

@ -118,8 +118,8 @@
(reagent.impl.batching/do-after-render (fn [] (trace/with-trace {:op-type :raf-end})))
(real-schedule)))))
(def total-traces (interop/ratom 0))
(def traces (interop/ratom []))
(defonce total-traces (interop/ratom 0))
(defonce traces (interop/ratom []))
(defn log-trace? [trace]
(let [rendering? (= (:op-type trace) :render)]
@ -173,7 +173,7 @@
showing? (rf/subscribe [:settings/show-panel?])
dragging? (r/atom false)
pin-to-bottom? (r/atom true)
selected-tab (r/atom (localstorage/get "selected-tab" :traces))
selected-tab (rf/subscribe [:settings/selected-tab])
window-width (r/atom js/window.innerWidth)
handle-window-resize (fn [e]
;; N.B. I don't think this should be a perf bottleneck.
@ -239,8 +239,7 @@
(case @selected-tab
:traces [traces/render-trace-panel traces]
:app-db [app-db/render-state db/app-db]
:subvis [subvis/render-subvis traces
[:div.panel-content-scrollable]]
:subvis [subvis/render-subvis traces]
[app-db/render-state db/app-db])]]]))})))
(defn panel-div []

View File

@ -1,6 +1,8 @@
(ns day8.re-frame.trace.events
(:require [mranderson047.re-frame.v0v10v2.re-frame.core :as rf]
[day8.re-frame.trace.utils.localstorage :as localstorage]))
[day8.re-frame.trace.utils.utils :as utils]
[day8.re-frame.trace.utils.localstorage :as localstorage]
[clojure.string :as str]))
(rf/reg-event-db
:settings/panel-width%
@ -27,12 +29,55 @@
(localstorage/save! "show-panel" show-panel?)
(assoc-in db [:settings :show-panel?] show-panel?))))
;; Traces
(defn save-filter-items [filter-items]
(localstorage/save! "filter-items" filter-items))
(rf/reg-event-db
:traces/filter-items
(fn [db [_ filter-items]]
(save-filter-items filter-items)
(assoc-in db [:traces :filter-items] filter-items)))
(rf/reg-event-db
:traces/add-filter
[(rf/path [:traces :filter-items])]
(fn [filter-items [_ filter-input filter-type]]
(let [new-db (when-not (some #(= filter-input (:query %)) filter-items) ;; prevent duplicate filter strings
;; if existing, remove prior filter for :slower-than
;; TODO: rework how time filters are used.
(when (and (= :slower-than filter-type)
(some #(= filter-type (:filter-type %)) filter-items))
(remove #(= :slower-than (:filter-type %)) filter-items))
;; add new filter
(conj filter-items {:id (random-uuid)
:query (if (= filter-type :contains)
(str/lower-case filter-input)
(js/parseFloat filter-input))
:filter-type filter-type}))]
(save-filter-items new-db)
new-db)))
(rf/reg-event-db
:traces/remove-filter
[(rf/path [:traces :filter-items])]
(fn [filter-items [_ filter-id]]
(let [new-db (remove #(= (:id %) filter-id) filter-items)]
(save-filter-items new-db)
new-db)))
(rf/reg-event-db
:traces/reset-filter-items
(fn [db _]
(let [new-db (utils/dissoc-in db [:traces :filter-items])]
(save-filter-items (get-in db :traces :filter-items))
new-db)))
;; App DB
(rf/reg-event-db
:app-db/paths
(fn [db [_ paths]]
(localstorage/save! "app-db-paths" paths)
(assoc-in db [:app-db :paths] paths)))

View File

@ -1,11 +1,12 @@
(ns day8.re-frame.trace.panels.app-db
(:require-macros [day8.re-frame.trace.macros :refer [with-cljs-devtools-prefs]])
(:require-macros [day8.re-frame.trace.utils.macros :refer [with-cljs-devtools-prefs]])
(:require [reagent.core :as r]
[clojure.string :as str]
[devtools.prefs]
[devtools.formatters.core]
[day8.re-frame.trace.localstorage :as localstorage]
[day8.re-frame.trace.components :as components]))
[day8.re-frame.trace.utils.localstorage :as localstorage]
[day8.re-frame.trace.components.components :as components]
[mranderson047.re-frame.v0v10v2.re-frame.core :as rf]))
(defn string->css [css-string]
"This function converts jsonml css-strings to valid css maps for hiccup.
@ -115,12 +116,8 @@
(defn render-state [data]
(let [subtree-input (r/atom "")
subtree-paths (r/atom (localstorage/get "subtree-paths" #{}))
subtree-paths (rf/subscribe [:app-db/paths])
input-error (r/atom false)]
(add-watch subtree-paths
:update-localstorage
(fn [_ _ _ new-state]
(localstorage/save! "subtree-paths" new-state)))
(fn []
[:div {:style {:flex "1 0 auto" :width "100%" :height "100%" :display "flex" :flex-direction "column"}}
[:div.panel-content-scrollable {:style {:margin 10}}
@ -132,7 +129,8 @@
(do
; (reset! input-error false)
;; TODO check if input already wrapped in braces
(swap! subtree-paths #(into #{(cljs.reader/read-string (str "[" path "]"))} %)))))
(rf/dispatch [:app-db/paths (into #{(cljs.reader/read-string (str "[" path "]"))} @subtree-paths)])
#_(swap! subtree-paths #(into #{(cljs.reader/read-string (str "[" path "]"))} %)))))
:on-change #(reset! subtree-input (.. % -target -value))}]]
; (if @input-error
; [:div.input-error {:style {:color "red" :margin-top 5}}

View File

@ -5,7 +5,8 @@
[reagent.core :as r]
[day8.re-frame.trace.utils.localstorage :as localstorage]
[cljs.pprint :as pprint]
[clojure.set :as set]))
[clojure.set :as set]
[mranderson047.re-frame.v0v10v2.re-frame.core :as rf]))
(defn query->fn [query]
(if (= :contains (:filter-type query))
@ -16,19 +17,7 @@
(< (:query query) (:duration trace)))))
(defn add-filter [filter-items filter-input filter-type]
;; prevent duplicate filter strings
(when-not (some #(= filter-input (:query %)) @filter-items)
;; if existing, remove prior filter for :slower-than
(when (and (= :slower-than filter-type)
(some #(= filter-type (:filter-type %)) @filter-items))
(swap! filter-items (fn [item]
(remove #(= :slower-than (:filter-type %)) item))))
;; add new filter
(swap! filter-items conj {:id (random-uuid)
:query (if (= filter-type :contains)
(str/lower-case filter-input)
(js/parseFloat filter-input))
:filter-type filter-type})))
(rf/dispatch [:traces/add-filter filter-input filter-type]))
(defn render-traces [visible-traces filter-items filter-input trace-detail-expansions]
(doall
@ -85,15 +74,11 @@
(defn render-trace-panel [traces]
(let [filter-input (r/atom "")
filter-items (r/atom (localstorage/get "filter-items" []))
filter-items (rf/subscribe [:traces/filter-items])
filter-type (r/atom :contains)
input-error (r/atom false)
categories (r/atom #{:event :sub/run :sub/create})
trace-detail-expansions (r/atom {:show-all? false :overrides {}})]
(add-watch filter-items
:update-localstorage
(fn [_ _ _ new-state]
(localstorage/save! "filter-items" new-state)))
(fn []
(let [toggle-category-fn (fn [category-keys]
(swap! categories #(if (set/superset? % category-keys)
@ -143,7 +128,7 @@
[:li.filter-item
[:button.button
{:style {:margin 0}
:on-click (fn [event] (swap! filter-items #(remove (comp (partial = (:query item)) :query) %)))}
:on-click #(rf/dispatch [:traces/remove-filter (:id item)])}
(:filter-type item) ": " [:span.filter-item-string (:query item)]]])
@filter-items)]]
[components/autoscroll-list {:class "panel-content-scrollable" :scroll? true}
@ -162,7 +147,7 @@
[:th
[:button {:class (str/join " " ["filter-items-count"
(when (pos? (count @filter-items)) "active")])
:on-click #(reset! filter-items [])}
:on-click #(rf/dispatch [:traces/reset-filter-items])}
(when (pos? (count @filter-items))
(str (count visible-traces) " of "))
(str (count @traces))]

View File

@ -15,3 +15,13 @@
:settings/selected-tab
(fn [db _]
(get-in db [:settings :selected-tab])))
(rf/reg-sub
:app-db/paths
(fn [db _]
(get-in db [:app-db :paths])))
(rf/reg-sub
:traces/filter-items
(fn [db _]
(get-in db [:traces :filter-items])))

View File

@ -0,0 +1,15 @@
(ns day8.re-frame.trace.utils.utils)
(defn dissoc-in
"Dissociates an entry from a nested associative structure returning a new
nested structure. keys is a sequence of keys. Any empty maps that result
will not be present in the new structure."
[m [k & ks :as keys]]
(if ks
(if-let [nextmap (clojure.core/get m k)]
(let [newmap (dissoc-in nextmap ks)]
(if (seq newmap)
(assoc m k newmap)
(dissoc m k)))
m)
(dissoc m k)))