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 possibility to unregister subscription/event handlers
  • Loading branch information
darwin committed Aug 17, 2015
commit a52d3600fcbdee9a87ce5f72a901167390baa545
2 changes: 2 additions & 0 deletions src/re_frame/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
(def router-loop scaffold/router-loop)
(def set-loggers! scaffold/set-loggers!)
(def register-sub scaffold/register-sub)
(def unregister-sub scaffold/unregister-sub)
(def clear-sub-handlers! scaffold/clear-sub-handlers!)
(def subscribe scaffold/subscribe)
(def clear-event-handlers! scaffold/clear-event-handlers!)
(def dispatch scaffold/dispatch)
(def dispatch-sync scaffold/dispatch-sync)
(def register-handler scaffold/register-handler)
(def unregister-handler scaffold/unregister-handler)

;(def pure scaffold/pure)
(def debug scaffold/debug)
Expand Down
34 changes: 26 additions & 8 deletions src/re_frame/frame.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ by the process doing actual transduction. See scaffold's transduce-by-resetting-
(let [{:keys [handlers loggers db-selector]} frame]
(fn [reducing-fn]
(fn
([] (reducing-fn)) ; transduction initialization, see [1]
([] (reducing-fn)) ; transduction init, see [1]
([result] (reducing-fn result)) ; transduction completion, see [1]
([state event]
([state event] ; transduction step, see [1]
(let [event-id (get-event-id event)
handler-fn (event-id handlers)]
(if (nil? handler-fn)
Expand Down Expand Up @@ -48,7 +48,7 @@ by the process doing actual transduction. See scaffold's transduce-by-resetting-
(-write writer ">")))

(defn make-frame
"Constructs independent frame instance."
"Constructs an independent frame instance."
([] (make-frame nil))
([handlers] (make-frame handlers nil))
([handlers subscriptions] (make-frame handlers subscriptions deref))
Expand All @@ -72,32 +72,50 @@ by the process doing actual transduction. See scaffold's transduce-by-resetting-
(handler-fn subscription-spec))))

(defn register-subscription-handler
"Registers a handler function for an id."
"Registers a subscription handler function for an id."
[frame subscription-id handler-fn]
(let [existing-subscriptions (get frame :subscriptions)]
(if (contains? existing-subscriptions subscription-id)
(let [warn (get-in frame [:loggers :warn])]
(warn "re-frame: overwriting subscription-handler for: " subscription-id))))
(warn "re-frame: overwriting subscription handler for: " subscription-id))))
(assoc-in frame [:subscriptions subscription-id] handler-fn))

(defn unregister-subscription-handler
"Unregisters subscription handler function previously registered via register-subscription-handler."
[frame subscription-id]
(let [existing-subscriptions (get frame :subscriptions)]
(if-not (contains? existing-subscriptions subscription-id)
(let [warn (get-in frame [:loggers :warn])]
(warn "re-frame: unregistering subscription handler \"" subscription-id "\" which does not exist."))))
(update frame :subscriptions dissoc subscription-id))

(defn clear-subscription-handlers
"Unregisters all subscription handlers."
[frame]
(assoc frame :subscriptions {}))
(assoc frame :subscriptions nil))

(defn register-event-handler
"Register a handler for an event."
([frame event-id handler-fn]
(let [existing-handlers (get frame :handlers)]
(if (contains? existing-handlers event-id)
(let [warn (get-in frame [:loggers :warn])]
(warn "re-frame: overwriting an event-handler for: " event-id)))
(warn "re-frame: overwriting an event handler for: " event-id)))
(assoc-in frame [:handlers event-id] handler-fn))))

(defn unregister-event-handler
"Unregisters event handler function previously registered via register-event-handler."
[frame event-id]
(let [existing-handlers (get frame :handlers)]
(if (contains? existing-handlers event-id)
(let [warn (get-in frame [:loggers :warn])]
(warn "re-frame: unregistering event handler \"" event-id "\" which does not exist."))))
(update frame :handlers dissoc event-id))

(defn clear-event-handlers
"Unregisters all event handlers."
[frame]
(assoc frame :handlers {}))
(assoc frame :handlers nil))

(defn set-loggers
"Resets loggers."
Expand Down
6 changes: 6 additions & 0 deletions src/re_frame/scaffold.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
(defn register-sub [subscription-id handler-fn]
(swap! app-frame #(frame/register-subscription-handler % subscription-id handler-fn)))

(defn unregister-sub [subscription-id]
(swap! app-frame #(frame/unregister-subscription-handler % subscription-id)))

(defn clear-sub-handlers! []
(swap! app-frame #(frame/clear-subscription-handlers %)))

Expand Down Expand Up @@ -106,6 +109,9 @@
(register-base event-id middleware handler)
(register-base event-id handler))))

(defn unregister-handler [event-id]
(swap! app-frame #(frame/unregister-event-handler % event-id)))

;; -- The Event Conveyor Belt --------------------------------------------------------------------
;;
;; Moves events from "dispatch" to the router loop.
Expand Down
30 changes: 25 additions & 5 deletions test/re_frame/test/frame.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
([result] result)
([_old-state new-state] new-state))]
(let [xform (frame/get-frame-transducer frame)]
(transduce xform reducing-fn nil [event]))))
((xform reducing-fn) nil event))))

(deftest frame-error-handling
(deftest frame-errors
(testing "doing invalid subscription"
(reset-log-recorder!)
(let [frame (make-empty-test-frame)]
Expand All @@ -66,21 +66,41 @@
(is (= (process-single-event frame [:non-existing-handler]) nil))
(is (= (last-error) ["re-frame: no event handler registered for: \"" :non-existing-handler "\". Ignoring."])))))

(deftest frame-warning-handling
(deftest frame-warnings
(testing "overwriting subscription handler"
(reset-log-recorder!)
(let [frame (make-empty-test-frame)
frame-with-some-handler (frame/register-subscription-handler frame :some-handler identity)]
(is (= (last-warn) nil))
(frame/register-subscription-handler frame-with-some-handler :some-handler (fn []))
(is (= (last-warn) ["re-frame: overwriting subscription-handler for: " :some-handler]))))
(is (= (last-warn) ["re-frame: overwriting subscription handler for: " :some-handler]))))
(testing "overwriting event handler"
(reset-log-recorder!)
(let [frame (make-empty-test-frame)
frame-with-some-handler (frame/register-event-handler frame :some-handler identity)]
(is (= (last-warn) nil))
(frame/register-event-handler frame-with-some-handler :some-handler (fn []))
(is (= (last-warn) ["re-frame: overwriting an event-handler for: " :some-handler])))))
(is (= (last-warn) ["re-frame: overwriting an event handler for: " :some-handler]))))
(testing "unregistering subscription handler which does not exist"
(reset-log-recorder!)
(let [frame (make-empty-test-frame)]
(is (= (last-warn) nil))
(frame/unregister-subscription-handler frame :non-existing-handler)
(is (= (last-warn) ["re-frame: unregistering subscription handler \"" :non-existing-handler "\" which does not exist."])))))

(deftest frame-event-handlers
(testing "unregister event handler"
(let [frame (-> (make-empty-test-frame)
(frame/register-event-handler :some-handler identity))]
(is (= (get-in frame [:handlers :some-handler]) identity))
(is (= (get-in (frame/unregister-event-handler frame :some-handler) [:handlers :some-handler] ::not-found) ::not-found)))))

(deftest frame-subscription-handlers
(testing "unregister subscription handler"
(let [frame (-> (make-empty-test-frame)
(frame/register-subscription-handler :some-handler identity))]
(is (= (get-in frame [:subscriptions :some-handler]) identity))
(is (= (get-in (frame/unregister-subscription-handler frame :some-handler) [:subscriptions :some-handler] ::not-found) ::not-found)))))

(deftest frame-transduction
(testing "simple transduce"
Expand Down