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

on-value callback for on-value subscriptions #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions src/com/degel/re_frame_firebase/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] []))
Expand Down