From 95b9b018ae3992bf79cb8db59f60f7398b04169c Mon Sep 17 00:00:00 2001 From: Erik Sandberg Date: Thu, 18 May 2017 13:11:27 -0700 Subject: [PATCH 1/3] Don't run on-changes if db is not in effects Currently, if there the effect handler does not set `:db`, then `on-changes` will end up treating `new-db` as `nil` and probably end up wiping the `db`. This causes the `on-changes` interceptor not to run if the `:db` is not set in the effect. --- src/re_frame/std_interceptors.cljc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/re_frame/std_interceptors.cljc b/src/re_frame/std_interceptors.cljc index a2d2add..235f949 100644 --- a/src/re_frame/std_interceptors.cljc +++ b/src/re_frame/std_interceptors.cljc @@ -294,7 +294,9 @@ ;; work out if any "inputs" have changed new-ins (map #(get-in new-db %) in-paths) old-ins (map #(get-in old-db %) in-paths) - changed-ins? (some false? (map identical? new-ins old-ins))] + ;; make sure the db is actually set in the effect + changed-ins? (and (contains? (get-effect context) :db) + (some false? (map identical? new-ins old-ins)))] ;; if one of the inputs has changed, then run 'f' (if changed-ins? From d33561c02e16a6deaa11c702835ec27db2d5291c Mon Sep 17 00:00:00 2001 From: Erik Sandberg Date: Thu, 18 May 2017 13:39:23 -0700 Subject: [PATCH 2/3] Update CHANGES.md --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 9d2a4c7..de9d6b0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,7 @@ ## Unreleased +- Fix `re-frame.std-interceptors/on-changes` to work even if effect handler does not set `:db`. [#341](https://github.com/Day8/re-frame/pull/341) + #### Breaking (previously undefined behaviour) - `reg-sub` enforces using `:<-` to indicate subscription inputs. Previously any keyword would have worked here. While using anything other than `:<-` was undefined behaviour previously, this could possibly break some code when upgrading. Thanks to [@Sohalt](https://github.com/Sohalt) [#336](https://github.com/Day8/re-frame/pull/336). From 50dea3bd678f92f5a78b7b35eac8968b3b3e0291 Mon Sep 17 00:00:00 2001 From: Erik Sandberg Date: Thu, 18 May 2017 13:55:58 -0700 Subject: [PATCH 3/3] Added test for no effect db in on-changes --- test/re-frame/interceptor_test.cljs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/re-frame/interceptor_test.cljs b/test/re-frame/interceptor_test.cljs index 4ebfc07..c1cc75d 100644 --- a/test/re-frame/interceptor_test.cljs +++ b/test/re-frame/interceptor_test.cljs @@ -106,8 +106,6 @@ (get-effect))] (is (= e {:db 5 :dispatch [:a]})))) - - (deftest test-on-changes (let [change-handler-i (-> (fn [db v] (assoc db :a 10)) db-handler->interceptor) @@ -115,6 +113,9 @@ no-change-handler-i (-> (fn [db v] db) db-handler->interceptor) + no-db-handler-i (-> (fn [ctx v] {}) + fx-handler->interceptor) + change-i (on-changes + [:c] [:a] [:b]) orig-db {:a 0 :b 2}] @@ -127,7 +128,13 @@ (-> (context [] [] orig-db) ((:before change-handler-i)) ;; cause change to :a ((:after change-i)) - (get-effect :db)))))) + (get-effect :db)))) + + (is (= ::not-found + (-> (context [] [] orig-db) + ((:before no-db-handler-i)) ;; no db effect in context + ((:after change-i)) + (get-effect :db ::not-found)))))) (deftest test-after (testing "when no db effect is returned"