Last active
August 29, 2015 14:03
-
-
Save verma/6cf76edffd91a93bac3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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