Skip to content

Instantly share code, notes, and snippets.

@verma
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verma/6cf76edffd91a93bac3e to your computer and use it in GitHub Desktop.
Save verma/6cf76edffd91a93bac3e to your computer and use it in GitHub Desktop.
;; walk-root walks the root node to a child node on firebase, e.g. /hello -> /hello/child/node
;; (walk-root root-node [:child :node])
;; bind is used like:
;; (bind root-node :value [:child :node] #(println "Changed to" %))
;;
;; (let [c (bind root-node :value [:child :node])]
;; (go-loop [m (<! c)]
;; (println "Changed to" m)
;; (recur (<! c))))
;;
(defn bind
"Bind to a certain property under the given root"
([root type korks]
(let [bc (chan)]
(pani.core/bind root type korks #(do
(println "Pushing" %)
(go (>! bc %))))
bc))
([root type korks cb]
(let [node (walk-root root korks)]
(cond
(= type :value)
(.addValueEventListener node
(reify com.firebase.client.ValueEventListener
(onDataChange [this v]
(cb (.getValue v)))))
(= type :child_added)
(.addChildEventListener node
(reify com.firebase.client.ChildEventListener
(onChildAdded [this v _]
(cb (.getValue v)))))
:else (throw (Exception. (str type " is not supported")))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment