Don't throw when subscribing to a non-existent sub

Fixes bug where we were logging if a sub didn't exist, but then trying
to create it still.
This commit is contained in:
Daniel Compton 2016-11-24 17:57:41 +13:00
parent 575051e7a7
commit ff164beb8f
3 changed files with 13 additions and 31 deletions

View File

@ -10,9 +10,13 @@
- [#200](https://github.com/Day8/re-frame/pull/200) Remove trailing spaces from console logging
- [#248](https://github.com/Day8/re-frame/pull/200) Provide after interceptor with `db` coeffect, if no `db` effect was produced.
- Add re-frame.loggers/get-loggers function to well, you know.
- [#259](https://github.com/Day8/re-frame/pull/259) Fix a bug where registering a subscription would create and close over dependent subscriptions, meaning that they would never be garbage collected, and doing more work than necessary.
- Added `clear-subscription-cache!` function. This should be used when hot reloading code to ensure that any bad subscriptions that cause rendering exceptions are removed. See [reagent-project/reagent#272](https://github.com/reagent-project/reagent/issues/272) for more details on this.
#### Fixes
- [#259](https://github.com/Day8/re-frame/pull/259) Fix a bug where registering a subscription would create and close over dependent subscriptions, meaning that they would never be garbage collected, and doing more work than necessary.
- Fix a bug where subscribing to a subscription that didn't exist would throw an exception, instead of returning nil.
## 0.8.0 (2016.08.19)
Staying on the leading edge of new buzzwords is obviously critical for any framework.

View File

@ -70,9 +70,9 @@
(let [query-id (first-in-vector query-v)
handler-fn (get-handler kind query-id)]
;(console :log "Subscription created: " query-v)
(if-not handler-fn
(console :error (str "re-frame: no subscription handler registered for: \"" query-id "\". Returning a nil subscription.")))
(cache-and-return query-v [] (handler-fn app-db query-v)))))
(if (nil? handler-fn)
(console :error (str "re-frame: no subscription handler registered for: \"" query-id "\". Returning a nil subscription."))
(cache-and-return query-v [] (handler-fn app-db query-v))))))
([v dynv]
(if-let [cached (cache-lookup v dynv)]

View File

@ -1,15 +1,15 @@
(ns re-frame.subs-test
(:require [cljs.test :refer-macros [is deftest]]
(:require [cljs.test :as test :refer-macros [is deftest]]
[reagent.ratom :refer-macros [reaction]]
[re-frame.subs :as subs]
[re-frame.db :as db]
[re-frame.core :as re-frame]))
(test/use-fixtures :each {:before (fn [] (subs/clear-all-handlers!))})
;=====test basic subscriptions ======
(deftest test-reg-sub
(subs/clear-all-handlers!)
(re-frame/reg-sub-raw
:test-sub
(fn [db [_]] (reaction (deref db))))
@ -20,8 +20,6 @@
(is (= 1 @test-sub))))
(deftest test-chained-subs
(subs/clear-all-handlers!)
(re-frame/reg-sub-raw
:a-sub
(fn [db [_]] (reaction (:a @db))))
@ -44,8 +42,6 @@
(is (= {:a 1 :b 3} @test-sub))))
(deftest test-sub-parameters
(subs/clear-all-handlers!)
(re-frame/reg-sub-raw
:test-sub
(fn [db [_ b]] (reaction [(:a @db) b])))
@ -56,8 +52,6 @@
(deftest test-sub-chained-parameters
(subs/clear-all-handlers!)
(re-frame/reg-sub-raw
:a-sub
(fn [db [_ a]] (reaction [(:a @db) a])))
@ -77,12 +71,13 @@
(reset! db/app-db {:a 1 :b 2})
(is (= {:a [1 :c], :b [2 :c]} @test-sub))))
(deftest test-nonexistent-sub
(is (nil? (re-frame/subscribe [:non-existence]))))
;============== test cached-subs ================
(def side-effect-atom (atom 0))
(deftest test-cached-subscriptions
(subs/clear-all-handlers!)
(reset! side-effect-atom 0)
(re-frame/reg-sub-raw
@ -106,8 +101,6 @@
;============== test register-pure macros ================
(deftest test-reg-sub-macro
(subs/clear-all-handlers!)
(subs/reg-sub
:test-sub
(fn [db [_]] db))
@ -118,8 +111,6 @@
(is (= 1 @test-sub))))
(deftest test-reg-sub-macro-singleton
(subs/clear-all-handlers!)
(subs/reg-sub
:a-sub
(fn [db [_]] (:a db)))
@ -138,8 +129,6 @@
(is (= {:a 1} @test-sub))))
(deftest test-reg-sub-macro-vector
(subs/clear-all-handlers!)
(subs/reg-sub
:a-sub
(fn [db [_]] (:a db)))
@ -163,8 +152,6 @@
(is (= {:a 1 :b 3} @test-sub))))
(deftest test-reg-sub-macro-map
(subs/clear-all-handlers!)
(subs/reg-sub
:a-sub
(fn [db [_]] (:a db)))
@ -188,8 +175,6 @@
(is (= {:a 1 :b 3} @test-sub))))
(deftest test-sub-macro-parameters
(subs/clear-all-handlers!)
(subs/reg-sub
:test-sub
(fn [db [_ b]] [(:a db) b]))
@ -199,8 +184,6 @@
(is (= [1 :c] @test-sub))))
(deftest test-sub-macros-chained-parameters
(subs/clear-all-handlers!)
(subs/reg-sub
:a-sub
(fn [db [_ a]] [(:a db) a]))
@ -222,8 +205,6 @@
(deftest test-sub-macros-<-
"test the syntactial sugar"
(subs/clear-all-handlers!)
(subs/reg-sub
:a-sub
(fn [db [_]] (:a db)))
@ -239,8 +220,6 @@
(deftest test-sub-macros-chained-parameters-<-
"test the syntactial sugar"
(subs/clear-all-handlers!)
(subs/reg-sub
:a-sub
(fn [db [_]] (:a db)))
@ -260,7 +239,6 @@
(is (= {:a 1 :b 2} @test-sub) )))
(deftest test-registering-subs-doesnt-create-subscription
(subs/clear-all-handlers!)
(let [sub-called? (atom false)]
(with-redefs [subs/subscribe (fn [& args] (reset! sub-called? true))]
(subs/reg-sub