Refactor match handling to return a map with multiple keys

This allows us to parse and collect other information when receiving
events, rather than calculating them at runtime when some information
may not be available (like dropped traces).
This commit is contained in:
Daniel Compton 2018-01-30 17:02:18 +13:00
parent 0994626814
commit 889bfc6a3a
3 changed files with 17 additions and 43 deletions

View File

@ -503,18 +503,12 @@
all-traces (reduce conj previous-traces filtered-traces) all-traces (reduce conj previous-traces filtered-traces)
parse-state (metam/parse-traces parse-state filtered-traces) parse-state (metam/parse-traces parse-state filtered-traces)
new-matches (:partitions parse-state) new-matches (:partitions parse-state)
previous-matches (get-in db [:epochs :matches-and-subs] []) previous-matches (get-in db [:epochs :matches] [])
parse-state (assoc parse-state :partitions []) ;; Remove matches we know about parse-state (assoc parse-state :partitions []) ;; Remove matches we know about
new-matches (remove (fn [match] new-matches (remove (fn [match]
(let [event (get-in (metam/matched-event match) [:tags :event])] (let [event (get-in (metam/matched-event match) [:tags :event])]
(contains? events-to-ignore (first event)))) new-matches) (contains? events-to-ignore (first event)))) new-matches)
sub-state {} sub-state {}
;; Retrieve previous state
;; Save new states, removing unretained ones (maybe by mapping with match?)
;; Save final state
subscription-match-state (rest subscription-match-state (rest
(reductions (fn [state match] (reductions (fn [state match]
(let [epoch-traces (into [] (let [epoch-traces (into []
@ -540,10 +534,10 @@
sub-state) sub-state)
(assoc :run? true (assoc :run? true
:value (:value tags)) :value (:value tags))
(update :order conj :sub/run)))) (update :order (fnil conj []) :sub/run))))
:sub/dispose (-> (assoc-in state [reaction-id :disposed?] true) :sub/dispose (-> (assoc-in state [reaction-id :disposed?] true)
(update-in [reaction-id :order] conj :sub/dispose)) (update-in [reaction-id :order] (fnil conj []) :sub/dispose))
(do (js/console.warn "Unhandled sub trace" trace) (do (js/console.warn "Unhandled sub trace, this is a bug, report to re-frame-trace please" trace)
state)))) state))))
reset-state)))) reset-state))))
sub-state sub-state
@ -556,7 +550,6 @@
start-of-epoch (nth epoch-traces 0) start-of-epoch (nth epoch-traces 0)
finish-run (or (first (filter metam/finish-run? epoch-traces)) finish-run (or (first (filter metam/finish-run? epoch-traces))
(utils/last-in-vec epoch-traces))] (utils/last-in-vec epoch-traces))]
;; Note, sometimes we still end up with a nil elapsed time here, look into more
{:re-frame/event-time (metam/elapsed-time start-of-epoch finish-run)})) {:re-frame/event-time (metam/elapsed-time start-of-epoch finish-run)}))
new-matches) new-matches)
@ -592,13 +585,11 @@
(assoc-in [:traces :all-traces] retained-traces) (assoc-in [:traces :all-traces] retained-traces)
(update :epochs (fn [epochs] (update :epochs (fn [epochs]
(assoc epochs (assoc epochs
:matches (into [] (map :match-info) retained-matches) :matches retained-matches
:matches-by-id (into {} (map (juxt first-match-id :match-info)) retained-matches) :matches-by-id (into {} (map (juxt first-match-id identity)) retained-matches)
:match-ids (mapv first-match-id retained-matches) :match-ids (mapv first-match-id retained-matches)
:parse-state parse-state :parse-state parse-state
:subscription-info subscription-info :subscription-info subscription-info)))))
:matches-and-subs retained-matches
:matches-by-id2 (into {} (map (juxt first-match-id identity)) retained-matches))))))
;; Else ;; Else
db))) db)))

View File

@ -210,7 +210,7 @@
(:epochs db))) (:epochs db)))
(rf/reg-sub (rf/reg-sub
:epochs/current-match :epochs/current-match-state
:<- [:epochs/epoch-root] :<- [:epochs/epoch-root]
:<- [:epochs/match-ids] :<- [:epochs/match-ids]
(fn [[epochs match-ids] _] (fn [[epochs match-ids] _]
@ -225,19 +225,10 @@
match))) match)))
(rf/reg-sub (rf/reg-sub
:epochs/current-match-state :epochs/current-match
:<- [:epochs/epoch-root] :<- [:epochs/current-match-state]
:<- [:epochs/match-ids] (fn [match-state _]
(fn [[epochs match-ids] _] (:match-info match-state)))
(let [current-id (:current-epoch-id epochs)
match (cond
(nil? current-id) (last (:matches-and-subs epochs))
(< current-id (first match-ids)) (first (:matches-and-subs epochs))
;; This case seems impossible, but can happen if the user filters out
;; an event that they are 'on'.
(> current-id (last match-ids)) (last (:matches-and-subs epochs))
:else (get (:matches-by-id2 epochs) current-id))]
match)))
(rf/reg-sub (rf/reg-sub
:epochs/current-event-trace :epochs/current-event-trace
@ -341,17 +332,8 @@
:timing/event-processing-time :timing/event-processing-time
:<- [:epochs/current-match-state] :<- [:epochs/current-match-state]
(fn [match] (fn [match]
(utils/spy "match" match)
(get-in match [:timing :re-frame/event-time]))) (get-in match [:timing :re-frame/event-time])))
#_(rf/reg-sub
:timing/event-processing-time
:<- [:traces/current-event-traces]
(fn [traces]
(let [start-of-epoch (nth traces 0)
finish-run (first (filter metam/finish-run? traces))]
(metam/elapsed-time start-of-epoch finish-run))))
(rf/reg-sub (rf/reg-sub
:timing/render-time :timing/render-time
:<- [:traces/current-event-traces] :<- [:traces/current-event-traces]

View File

@ -16,14 +16,15 @@
[rc/label :label "Epochs"] [rc/label :label "Epochs"]
(let [current-match @(rf/subscribe [:epochs/current-match])] (let [current-match @(rf/subscribe [:epochs/current-match])]
(for [match (:matches @(rf/subscribe [:epochs/epoch-root]))] (for [match (:matches @(rf/subscribe [:epochs/epoch-root]))
^{:key (:id (first match))} :let [match-info (:match-info match)]]
^{:key (:id (first match-info))}
[rc/v-box [rc/v-box
:style {:border "1px solid black" :style {:border "1px solid black"
:font-weight (if (= current-match match) :font-weight (if (= current-match match-info)
"bold" "bold"
"normal")} "normal")}
:children (doall (map (fn [event] [rc/label :label (prn-str event)]) (metam/summarise-match match))) :children (doall (map (fn [event] [rc/label :label (prn-str event)]) (metam/summarise-match match-info)))
])) ]))
]] ]]
) )