diff --git a/src/day8/re_frame/trace/events.cljs b/src/day8/re_frame/trace/events.cljs index 9941de5..6dd71af 100644 --- a/src/day8/re_frame/trace/events.cljs +++ b/src/day8/re_frame/trace/events.cljs @@ -508,10 +508,53 @@ 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 [] + (comp + (utils/id-between-xf (:id (first match)) (:id (last match))) + (filter metam/subscription?)) + filtered-traces) + reset-state (into {} + (map (fn [[k v]] + [k (dissoc v :order :created? :run? :disposed? :previous-value)])) + state)] + (->> epoch-traces + (reduce (fn [state trace] + (let [tags (get trace :tags) + reaction-id (:reaction tags)] + (case (:op-type trace) + :sub/create (assoc state reaction-id {:created? true + :subscription (:query-v tags) + :order [:sub/create]}) + :sub/run (update state reaction-id (fn [sub-state] + (-> (if (contains? sub-state :value) + (assoc sub-state :previous-value (:value sub-state)) + sub-state) + (assoc :run? true + :value (:value tags)) + (update :order 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) + state)))) + reset-state)))) + sub-state + new-matches)) + + ;_ (js/console.log new-matches subscription-match-state) + new-matches (map (fn [match sub-match] match) new-matches subscription-match-state) all-matches (reduce conj previous-matches new-matches) retained-matches (into [] (take-last number-of-epochs-to-retain all-matches)) app-db-id (get-in db [:app-db :reagent-id]) - subscription-info (->> new-traces + subscription-info (->> filtered-traces (filter metam/subscription-re-run?) (reduce (fn [state trace] ;; Can we take any shortcuts by assuming that a sub with diff --git a/src/day8/re_frame/trace/subs.cljs b/src/day8/re_frame/trace/subs.cljs index ed88f5e..a806c46 100644 --- a/src/day8/re_frame/trace/subs.cljs +++ b/src/day8/re_frame/trace/subs.cljs @@ -151,7 +151,7 @@ :<- [:epochs/beginning-trace-id] :<- [:epochs/ending-trace-id] (fn [[traces beginning ending] _] - (into [] (filter #(<= beginning (:id %) ending)) traces))) + (into [] (utils/id-between-xf beginning ending) traces))) (defn filter-ignored-views [[traces filtered-views] _] (let [munged-ns (->> filtered-views diff --git a/src/day8/re_frame/trace/utils/utils.cljs b/src/day8/re_frame/trace/utils/utils.cljs index 850b0fc..85153eb 100644 --- a/src/day8/re_frame/trace/utils/utils.cljs +++ b/src/day8/re_frame/trace/utils/utils.cljs @@ -16,3 +16,8 @@ "Gets the index of the first item in vec that matches the predicate" [pred v] (first (find-all-indexes-in-vec pred v))) + +(defn id-between-xf + "Returns a transducer that filters for :id between beginning and ending." + [beginning ending] + (filter #(<= beginning (:id %) ending)))