From dc82af6fe73793598f9e62104747b79b7aa7f145 Mon Sep 17 00:00:00 2001 From: Antonin Hildebrand Date: Wed, 19 Aug 2015 16:36:56 +0200 Subject: [PATCH] refactor resetting atoms https://github.com/Day8/re-frame/pull/107#discussion_r37421196 --- src/re_frame/frame.cljs | 22 +++++++++------------- src/re_frame/utils.cljs | 6 +++++- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/re_frame/frame.cljs b/src/re_frame/frame.cljs index 38529183c..d90672807 100644 --- a/src/re_frame/frame.cljs +++ b/src/re_frame/frame.cljs @@ -1,5 +1,5 @@ (ns re-frame.frame - (:require [re-frame.utils :refer [get-event-id get-subscription-id simple-inflection frame-summary-description]] + (:require [re-frame.utils :refer [get-event-id get-subscription-id simple-inflection frame-summary-description reset-if-changed!]] [re-frame.logging :refer [log warn error default-loggers]])) ; re-frame meat implemented in terms of pure functions (with help of transducers) @@ -111,6 +111,8 @@ state) (reducing-fn state new-db)))))))))) ; reducing function prepares new transduction state +; TODO: we should memoize this function, beause it will be usually called with same frame +; using something like https://github.com/clojure/core.memoize with LRU cache would be neat (defn get-frame-transducer "Returns a transducer: state, event -> state. This transducer resolves event-id, selects matching handler and calls it with old db state to produce a new db state. @@ -136,31 +138,25 @@ by the process doing actual transduction. See event processing helpers below for (transduce xform reducing-fn [init-db] events))) (defn process-event-on-atom [frame db-atom event] - (let [old-db @db-atom - new-db (process-event frame old-db event)] - (if-not (identical? old-db new-db) - (reset! db-atom new-db)))) + (let [new-db (process-event frame @db-atom event)] + (reset-if-changed! db-atom new-db))) (defn process-events-on-atom [frame db-atom events] (let [reducing-fn (fn ([db-atom] db-atom) ; completion ([db-atom new-db] ; in each step - (let [old-db @db-atom] ; commit new-db to atom - (if-not (identical? old-db new-db) - (reset! db-atom new-db))) + (reset-if-changed! db-atom new-db) ; commit new-db to atom db-atom)) xform (get-frame-transducer frame deref)] (transduce xform reducing-fn db-atom events))) (defn process-events-on-atom-with-coallesced-write [frame db-atom events] - (let [old-db @db-atom - reducing-fn (fn + (let [reducing-fn (fn ([final-db] ; completion - (if-not (identical? old-db final-db) ; commit final-db to atom - (reset! db-atom final-db))) + (reset-if-changed! db-atom final-db)) ; commit final-db to atom ([_old-db new-db] new-db)) ; simply carry-on with new-db as our new state xform (get-frame-transducer frame identity)] - (transduce xform reducing-fn old-db events))) + (transduce xform reducing-fn @db-atom events))) ;; -- nice to have ----------------------------------------------------------------------------------------------------- diff --git a/src/re_frame/utils.cljs b/src/re_frame/utils.cljs index 3c57349d4..11471b33c 100644 --- a/src/re_frame/utils.cljs +++ b/src/re_frame/utils.cljs @@ -23,6 +23,10 @@ handlers-count " " (simple-inflection "handler" handlers-count) ", " subscriptions-count " " (simple-inflection "subscription" subscriptions-count)))) +(defn reset-if-changed! [db-atom new-db-state] + (if-not (identical? @db-atom new-db-state) + (reset! db-atom new-db-state))) + ;; -- composing middleware ----------------------------------------------------------------------- (defn report-middleware-factories @@ -53,4 +57,4 @@ (apply comp middlewares)) :else (do (warn frame "re-frame: comp-middleware expects a vector, got: " what) - nil)))) + nil)))) \ No newline at end of file