Last active
August 26, 2015 20:14
-
-
Save wildermuthn/447b482b6e29d1bedac2 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
(ns fncards.db.utils | |
(:require [fncards.db.api :as db-api] | |
[datascript :as ds] | |
[cljs.pprint :refer [pprint]] | |
[cljs.core.async :as async :refer [chan <! >! put! take! timeout mult tap pub sub]]) | |
(:require-macros [cljs.core.async.macros :refer [go go-loop]] | |
[fncards.macros :refer [log logr logd logc]])) | |
;; Channel and Subscription | |
(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)) | |
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
(ns fncards.db.views | |
(:require | |
[fncards.db.utils :as db-utils] | |
[fncards.db.api :as db-api] | |
[datascript :as ds] | |
[cljs.pprint :refer [pprint]] | |
[cljs.core.async :as async :refer [chan <! >! put! take! timeout mult tap pub sub]]) | |
(:require-macros [cljs.core.async.macros :refer [go go-loop]] | |
[fncards.macros :refer [log logr logd logc]])) | |
(defonce db db-api/app-db) | |
(defn updates [] | |
(db-utils/get-updates #(:view/current %))) | |
(defn change [k] | |
(ds/transact! db [{:view/current k}])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment