Remove ratom state and centralise it in re-frame's app-db

This commit is contained in:
Daniel Compton 2018-01-27 22:50:36 +13:00
parent f20dd1b0d0
commit ddcee6b2c8
1 changed files with 42 additions and 40 deletions

View File

@ -17,9 +17,6 @@
(def default-number-of-epochs-to-retain 5)
(defonce traces (r/atom []))
(defonce total-traces (r/atom 0))
(defonce number-of-epochs-to-retain (atom default-number-of-epochs-to-retain))
(defonce events-to-ignore (atom #{}))
(defn log-trace? [trace]
(let [render-operation? (or (= (:op-type trace) :render)
@ -33,33 +30,7 @@
(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)
(sort-by :id))]
(swap! total-traces + (count new-traces))
(swap! traces
(fn [existing]
(let [new (reduce conj existing new-traces)
size (count new)]
(if (< 8000 size)
(let [new2 (subvec new (- size 4000))]
(if (< @total-traces 40000) ;; Create a new vector to avoid structurally sharing all traces forever
(do (reset! total-traces 0)
(into [] new2))))
new))))
;; TODO: there is a bit of double handling here with retaining the last n epochs,
;; that will be cleaned up when the epoch parsing is refactored.
(let [matches (:matches (metam/parse-traces @traces))
matches (remove (fn [match]
(let [event (get-in (metam/matched-event match) [:tags :event])]
(contains? @events-to-ignore (first event)))) matches)
retained-epochs (take-last @number-of-epochs-to-retain matches)
first-id-to-retain (:id (ffirst retained-epochs))
new-traces (into [] (drop-while #(< (:id %) first-id-to-retain)) @traces)]
(reset! traces new-traces)
(reset! total-traces (count new-traces))
(rf/dispatch [:traces/update-traces new-traces])
(rf/dispatch [:epochs/update-epochs {:matches retained-epochs}]))))))
(re-frame.trace/register-trace-cb ::cb #(rf/dispatch [:epochs/receive-new-traces %])))
(defn dissoc-in
"Dissociates an entry from a nested associative structure returning a new
@ -110,13 +81,6 @@
(js/location.reload)
db))
(rf/reg-event-db
:settings/clear-epochs
(fn [db _]
(reset! traces [])
(reset! total-traces 0)
db))
(rf/reg-event-db
:settings/user-toggle-panel
(fn [db _]
@ -158,12 +122,11 @@
num (if (and (not (js/isNaN num)) (pos-int? num))
num
default-number-of-epochs-to-retain)]
(reset! number-of-epochs-to-retain num)
(localstorage/save! "retained-epochs" num)
(assoc-in db [:settings :number-of-epochs] num))))
(def ignored-event-mw
[(rf/path [:settings :ignored-events]) (rf/after #(localstorage/save! "ignored-events" %)) (rf/after #(reset! events-to-ignore (->> % vals (map :event-id) set)))])
[(rf/path [:settings :ignored-events]) (rf/after #(localstorage/save! "ignored-events" %))])
(rf/reg-event-db
:settings/add-ignored-event
@ -523,6 +486,46 @@
[m]
(-> m first :id))
(rf/reg-event-db
:epochs/receive-new-traces
(fn [db [_ new-traces]]
(when-let [new-traces (->> (filter log-trace? new-traces)
(sort-by :id))]
(let [number-of-epochs-to-retain (get-in db [:settings :number-of-epochs])
events-to-ignore (->> (get-in db [:settings :ignored-events]) vals (map :event-id) set)
#_ #_ total-traces (get-in db [:traces :total-traces])
#_ #_ new-total-traces (+ total-traces (count new-traces))]
#_(swap! total-traces + (count new-traces))
;; TODO: add this back in once the algorithm is better defined
#_(swap! traces
(fn [existing]
(let [new (reduce conj existing new-traces)
size (count new)]
(if (< 8000 size)
(let [new2 (subvec new (- size 4000))]
(if (< new-total-traces 40000) ;; Create a new vector to avoid structurally sharing all traces forever
(do (comment "Total traces set to 0") ;(reset! total-traces 0)
(into [] new2))))
new))))
(swap! traces (fn [existing] (reduce conj existing new-traces)))
;; TODO: there is a bit of double handling here with retaining the last n epochs,
;; that will be cleaned up when the epoch parsing is refactored.
(let [matches (:matches (metam/parse-traces @traces))
matches (remove (fn [match]
(let [event (get-in (metam/matched-event match) [:tags :event])]
(contains? events-to-ignore (first event)))) matches)
retained-epochs (take-last number-of-epochs-to-retain matches)
first-id-to-retain (:id (ffirst retained-epochs))
new-traces (into [] (drop-while #(< (:id %) first-id-to-retain)) @traces)]
(reset! traces new-traces)
;; Reset total-traces count to new traces
#_(reset! total-traces (count new-traces))
(rf/dispatch [:traces/update-traces new-traces])
(rf/dispatch [:epochs/update-epochs {:matches retained-epochs}]))))
db))
(rf/reg-event-db
:epochs/update-epochs
[(rf/path [:epochs])]
@ -564,7 +567,6 @@
(fn [db]
(re-frame.trace/reset-tracing!)
(reset! traces [])
(reset! total-traces 0)
(dissoc db :epochs :traces)))
(rf/reg-event-db