diff --git a/README.md b/README.md index c67e628..49594d0 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,14 @@ _Internal detail: The values are currently cached in your app db, under the key `com.degel.re-frame-firebase.core\cache`. But, this is an implementation detail, subject to change. Please do not rely on this for anything except, perhaps, debugging._ +The subscription support additional keys: + +- `on-failure` - called when Firebase `:on` subscription error occurs (e.g. due to lack of + permission to read the data) +- `on-value` - called with the inital value and again whenever the value changes. It might +be useful to trigger an event which will store the value in the `app-db` so it's accessible +by event handlers. + ## Setup diff --git a/src/com/degel/re_frame_firebase/core.cljs b/src/com/degel/re_frame_firebase/core.cljs index 4881bd3..2d49be7 100644 --- a/src/com/degel/re_frame_firebase/core.cljs +++ b/src/com/degel/re_frame_firebase/core.cljs @@ -69,7 +69,7 @@ #((event->fn on-failure) %))) -(defn firebase-on-value-sub [app-db [_ {:keys [path on-failure]}]] +(defn firebase-on-value-sub [app-db [_ {:keys [path on-value on-failure]}]] (let [ref (fb-ref path) ;; [TODO] Potential bug alert: ;; We are caching the results, keyed only by path, and we clear @@ -83,7 +83,11 @@ ;; (modulo some reflection hack), since we use the id as part of ;; the callback closure. id path - callback #(>evt [::on-value-handler id (js->clj-tree %)])] + callback (fn [value] + (let [value (js->clj-tree value)] + (when on-value + (>evt [on-value value])) + (>evt [::on-value-handler id value])))] (.on ref "value" callback (event->fn (or on-failure (default-error-handler)))) (rv/make-reaction (fn [] (get-in @app-db [::cache id] []))