Skip to content

Instantly share code, notes, and snippets.

@swannodette
Last active December 19, 2015 11:09
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swannodette/5945417 to your computer and use it in GitHub Desktop.
Save swannodette/5945417 to your computer and use it in GitHub Desktop.
(def cds (collection))
;; interact with database
(go
(>! (:in cds)
{:op :create
:val {:title "Soft Machine Vol. 1"
:artist "Soft Machine"
:year 1969}})
(>! (:in cds)
{:op :create
:val {:title "Marble Index"
:artist "Nico"
:year 1969}}))
;; listen to stream of all db events
(let [stream (subscribe (:events cds) (chan))]
(go-loop
(println "EVENT LISTEN:" (<! stream))))
(defn collection
([] (collection (chan)
(chan (sliding-buffer 10)) (chan (sliding-buffer 10)) {}))
([in out events coll]
(go
(loop [coll coll cid 0 e nil]
(when e
(>! events e))
(let [{:keys [op id val]} (<! in)]
(condp = op
:query (do (>! out (into {} (filter f coll)))
(recur coll cid nil))
:create (do (>! out cid)
(let [x (assoc val :id cid)]
(recur
(assoc coll cid x)
(inc cid) [:create x])))
:read (do (>! out (get coll id))
(recur coll cid [:read id]))
:update (recur (assoc coll id val) cid [:update id])
:delete (recur (dissoc coll id) cid [:delete id])))))
{:in in
:out out
:events (observable events)}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment