Skip to content

Commit

Permalink
refactor resetting atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Aug 19, 2015
1 parent 22e91e1 commit dc82af6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
22 changes: 9 additions & 13 deletions src/re_frame/frame.cljs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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 -----------------------------------------------------------------------------------------------------

Expand Down
6 changes: 5 additions & 1 deletion src/re_frame/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,4 +57,4 @@
(apply comp middlewares))
:else (do
(warn frame "re-frame: comp-middleware expects a vector, got: " what)
nil))))
nil))))

This comment has been minimized.

Copy link
@thenonameguy

thenonameguy Aug 19, 2015

Missing newline at EOF.

EDIT: Great job by the way, and the thanks for the refactor!

0 comments on commit dc82af6

Please sign in to comment.