Created
September 3, 2015 22:22
-
-
Save wildermuthn/4c173c8bf545c3a6d5fb 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
(defn- build-handle-tx | |
"On db update, put updated entity onto update-ch" | |
[ch db] | |
(fn | |
[{:keys [tx-data]}] | |
(let [id (-> tx-data first first) | |
e (ds/entity @db (-> tx-data first first))] | |
(if e (go (>! ch e)))))) | |
(defn get-updates | |
"Returns a channel that returns changes to db" | |
[f] | |
(let [db db-api/app-db | |
entity-ch (chan) | |
handle-tx (build-handle-tx entity-ch db) | |
ch (chan)] | |
(go-loop [entity false] | |
(when entity | |
(>! ch entity)) | |
(recur (f (<! entity-ch)))) | |
(ds/listen! db-api/app-db (keyword (gensym "get-update-fn")) handle-tx) | |
ch)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment