From 889bfc6a3a750dcbc03d852a3c00ddd8eef09a10 Mon Sep 17 00:00:00 2001 From: Daniel Compton Date: Tue, 30 Jan 2018 17:02:18 +1300 Subject: [PATCH] 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). --- src/day8/re_frame/trace/events.cljs | 23 +++++++------------- src/day8/re_frame/trace/subs.cljs | 28 +++++-------------------- src/day8/re_frame/trace/view/debug.cljs | 9 ++++---- 3 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/day8/re_frame/trace/events.cljs b/src/day8/re_frame/trace/events.cljs index 97fa1f4..9291815 100644 --- a/src/day8/re_frame/trace/events.cljs +++ b/src/day8/re_frame/trace/events.cljs @@ -503,18 +503,12 @@ all-traces (reduce conj previous-traces filtered-traces) parse-state (metam/parse-traces parse-state filtered-traces) 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 new-matches (remove (fn [match] (let [event (get-in (metam/matched-event match) [:tags :event])] (contains? events-to-ignore (first event)))) new-matches) sub-state {} - - ;; Retrieve previous state - ;; Save new states, removing unretained ones (maybe by mapping with match?) - ;; Save final state - - subscription-match-state (rest (reductions (fn [state match] (let [epoch-traces (into [] @@ -540,10 +534,10 @@ sub-state) (assoc :run? true :value (:value tags)) - (update :order conj :sub/run)))) + (update :order (fnil conj []) :sub/run)))) :sub/dispose (-> (assoc-in state [reaction-id :disposed?] true) - (update-in [reaction-id :order] conj :sub/dispose)) - (do (js/console.warn "Unhandled sub trace" trace) + (update-in [reaction-id :order] (fnil conj []) :sub/dispose)) + (do (js/console.warn "Unhandled sub trace, this is a bug, report to re-frame-trace please" trace) state)))) reset-state)))) sub-state @@ -556,7 +550,6 @@ start-of-epoch (nth epoch-traces 0) finish-run (or (first (filter metam/finish-run? 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)})) new-matches) @@ -592,13 +585,11 @@ (assoc-in [:traces :all-traces] retained-traces) (update :epochs (fn [epochs] (assoc epochs - :matches (into [] (map :match-info) retained-matches) - :matches-by-id (into {} (map (juxt first-match-id :match-info)) retained-matches) + :matches retained-matches + :matches-by-id (into {} (map (juxt first-match-id identity)) retained-matches) :match-ids (mapv first-match-id retained-matches) :parse-state parse-state - :subscription-info subscription-info - :matches-and-subs retained-matches - :matches-by-id2 (into {} (map (juxt first-match-id identity)) retained-matches)))))) + :subscription-info subscription-info))))) ;; Else db))) diff --git a/src/day8/re_frame/trace/subs.cljs b/src/day8/re_frame/trace/subs.cljs index 4873c73..3443f7c 100644 --- a/src/day8/re_frame/trace/subs.cljs +++ b/src/day8/re_frame/trace/subs.cljs @@ -210,7 +210,7 @@ (:epochs db))) (rf/reg-sub - :epochs/current-match + :epochs/current-match-state :<- [:epochs/epoch-root] :<- [:epochs/match-ids] (fn [[epochs match-ids] _] @@ -225,19 +225,10 @@ match))) (rf/reg-sub - :epochs/current-match-state - :<- [:epochs/epoch-root] - :<- [:epochs/match-ids] - (fn [[epochs match-ids] _] - (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))) + :epochs/current-match + :<- [:epochs/current-match-state] + (fn [match-state _] + (:match-info match-state))) (rf/reg-sub :epochs/current-event-trace @@ -341,17 +332,8 @@ :timing/event-processing-time :<- [:epochs/current-match-state] (fn [match] - (utils/spy "match" match) (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 :timing/render-time :<- [:traces/current-event-traces] diff --git a/src/day8/re_frame/trace/view/debug.cljs b/src/day8/re_frame/trace/view/debug.cljs index 231e6d7..a7ebe10 100644 --- a/src/day8/re_frame/trace/view/debug.cljs +++ b/src/day8/re_frame/trace/view/debug.cljs @@ -16,14 +16,15 @@ [rc/label :label "Epochs"] (let [current-match @(rf/subscribe [:epochs/current-match])] - (for [match (:matches @(rf/subscribe [:epochs/epoch-root]))] - ^{:key (:id (first match))} + (for [match (:matches @(rf/subscribe [:epochs/epoch-root])) + :let [match-info (:match-info match)]] + ^{:key (:id (first match-info))} [rc/v-box :style {:border "1px solid black" - :font-weight (if (= current-match match) + :font-weight (if (= current-match match-info) "bold" "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))) ])) ]] )