Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decouple re-frame and reagent+core.async #107

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
0e182a2
decouple re-frame and reagent+core.async
darwin Aug 15, 2015
a54eaf1
forgot to remove extra apply
darwin Aug 16, 2015
200aef2
remove debug logging
darwin Aug 16, 2015
6bf1aba
fix broken project repo url
darwin Aug 17, 2015
6e9eefd
bump project version to 0.5.0
darwin Aug 17, 2015
b7f78b8
minor refinements in tests
darwin Aug 17, 2015
0f4fcd6
bump todomvc deps
darwin Aug 17, 2015
5ee36de
add checkouts so that todomvc uses local sources
darwin Aug 17, 2015
329a57f
fix comp-middleware
darwin Aug 17, 2015
b62e3ab
integrate cljs-devtools into todomvc
darwin Aug 17, 2015
072ac10
integrate cljs-devtools into simpleexample
darwin Aug 17, 2015
bae4df8
move legacy-subscribe to scaffold
darwin Aug 17, 2015
bd4602f
get-frame-transducer does the work directly
darwin Aug 17, 2015
dc0981a
provide no-loggers for convenience
darwin Aug 17, 2015
c05223f
default loggers should apply all args
darwin Aug 17, 2015
2778d5a
switch frame implementation to use defrecord
darwin Aug 17, 2015
3d72b79
enable figwheel nrepl in examples
darwin Aug 17, 2015
addfb80
rename frame-factory to make-frame
darwin Aug 17, 2015
c09f3c6
minor refinements in core tests
darwin Aug 17, 2015
7a8156d
properly override protocol on defrecord
darwin Aug 17, 2015
568cdd3
move "handler returned nil" check into transducer
darwin Aug 17, 2015
751ccfb
add test "calling a handler which does not exist"
darwin Aug 17, 2015
a56a6e7
give our transducer some love
darwin Aug 17, 2015
173e824
move frame-summary-description to utils
darwin Aug 17, 2015
a52d360
add possibility to unregister subscription/event handlers
darwin Aug 17, 2015
4137a0d
in case of processing only one event we can use transducer directly
darwin Aug 17, 2015
12e9b66
better logging helpers
darwin Aug 17, 2015
049285b
return pure handler for compatibility reasons
darwin Aug 17, 2015
e9f525d
fix a typo
darwin Aug 17, 2015
9b31889
DRY logger helpers
darwin Aug 17, 2015
5f4be3c
use defonce to make scaffold figwheel reloadable
darwin Aug 17, 2015
4a7873d
various tweaks to increase reusability of scaffold code
darwin Aug 17, 2015
f393cc4
move transduce-event(s)-by-resetting-atom to utils
darwin Aug 17, 2015
5ff3204
transducer factory: transducers are parametrized by db-selector
darwin Aug 17, 2015
14314dd
add tests to exercise event processing
darwin Aug 17, 2015
e534e6a
frame polishing
darwin Aug 17, 2015
ff346a8
add frame/process-events
darwin Aug 18, 2015
8f61e93
move log recording helpers under test.utils.log-recording ns
darwin Aug 18, 2015
911ab0f
add tests to exercise triggering subscriptions
darwin Aug 18, 2015
df66bf5
when triggering subscription make sure sub-id matches
darwin Aug 18, 2015
22e91e1
DRY definition of recording-loggers
darwin Aug 18, 2015
dc82af6
refactor resetting atoms
darwin Aug 19, 2015
6ad431f
mark side-effecting functions as such
darwin Aug 19, 2015
069b7cc
missing newline before EOF in utils.cljs
darwin Aug 19, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add frame/process-events
  • Loading branch information
darwin committed Aug 18, 2015
commit ff346a866be689be091f674f775cadb396ab1054
30 changes: 19 additions & 11 deletions src/re_frame/frame.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ by the process doing actual transduction. See event processing helpers below for

;; -- event processing -------------------------------------------------------------------------------------------------

(defn process-event [frame init-db event]
(let [reducing-fn (fn [_old-state new-state] new-state)
xform (get-frame-transducer frame identity)]
((xform reducing-fn) init-db event)))

(defn process-events [frame init-db events]
(let [reducing-fn (fn
([db-states] db-states) ; completion
([db-states new-db] ; in each step
(conj db-states new-db))) ; add new-db state to the vector
xform (get-frame-transducer frame last)]
(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))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern:

(if-not (identitcal? old-db new-db)
  (reset! db-atom new-db))

is appearing in quite a few places, it might be better refactor it out to a separate fn:

(defn reset-if-changed! [db-atom old-db new-db] (...))


(defn process-events-on-atom [frame db-atom events]
(let [reducing-fn (fn
([db-atom] db-atom) ; completion
Expand All @@ -143,17 +162,6 @@ by the process doing actual transduction. See event processing helpers below for
xform (get-frame-transducer frame identity)]
(transduce xform reducing-fn old-db events)))

(defn process-event [frame old-db event]
(let [reducing-fn (fn [_old-state new-state] new-state)
xform (get-frame-transducer frame identity)]
((xform reducing-fn) old-db event)))

(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))))

;; -- nice to have -----------------------------------------------------------------------------------------------------

(extend-protocol IPrintWithWriter
Expand Down
7 changes: 7 additions & 0 deletions test/re_frame/test/frame.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@
(frame/register-event-handler :my-handler my-handler))
result (frame/process-event frame "[initial state]" [:my-handler 1 2])]
(is (= result "state:[initial state] args:(:my-handler 1 2)"))))
(testing "process multiple events, get vector of states back"
(let [init-db 0
add-handler (fn [state [_event-id num]] (+ state num))
frame (-> (make-empty-test-frame)
(frame/register-event-handler :add add-handler))
result (frame/process-events frame init-db [[:add 1] [:add 2] [:add 10]])]
(is (= result [0 1 3 13]))))
(testing "process event on atom"
(let [db (atom 0)
reset-counter (atom 0)
Expand Down