Skip to content

Commit

Permalink
Pipe queries to atoms directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Gómez committed Sep 20, 2015
1 parent 389c90b commit faded28
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 125 deletions.
66 changes: 25 additions & 41 deletions src/uxbox/queries.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@

(defn rquery
[q conn]
(let [k (gensym)]
(s/from-binder (fn [sink]
;; put first
(sink (query q @conn))
;; put subsequent
(d/listen! conn
k
(fn [txr]
(when-let [after (eids-changed? q txr)]
(sink after))))
;; unsub fn
(fn []
(d/unlisten! conn k))))))
(let [k (gensym)
a (atom (query q @conn))
sink #(reset! a %)]
(d/listen! conn
k
(fn [txr]
(when-let [after (eids-changed? q txr)]
(sink after))))
a))

;; reactive pull

Expand All @@ -46,20 +42,14 @@
(defn rpull
[q p conn]
(let [k (gensym)
pulls
(s/from-binder (fn [sink]
;; put first
(sink (pull-one-or-many (query q @conn) p @conn))
;; put subsequent
(d/listen! conn
k
(fn [txr]
(let [after (query q (:db-after txr))]
(sink (pull-one-or-many after p (:db-after txr))))))
;; unsub fn
(fn []
(d/unlisten! conn k))))]
(s/dedupe pulls)))
a (atom (pull-one-or-many (query q @conn) p @conn))
sink #(reset! a %)]
(d/listen! conn
k
(fn [txr]
(let [after (query q (:db-after txr))]
(sink (pull-one-or-many after p (:db-after txr))))))
a))

;; reactive entity

Expand All @@ -79,17 +69,11 @@
(rentity id '[*] conn))
([id p conn]
(let [k (gensym)
pulls
(s/from-binder (fn [sink]
;; put first
(sink (pull-entity id p @conn))
;; put subsequent
(d/listen! conn
k
(fn [txr]
(when-let [e (entity-changed? id p txr)]
(sink e))))
;; unsub fn
(fn []
(d/unlisten! conn k))))]
(s/dedupe pulls))))
a (atom (pull-entity id p @conn))
sink #(reset! a %)]
(d/listen! conn
k
(fn [txr]
(when-let [e (entity-changed? id p txr)]
(sink e))))
a)))
10 changes: 5 additions & 5 deletions src/uxbox/ui/mixins.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
(:require
[rum]
[datascript :as d]
[uxbox.queries :as sq]
[uxbox.streams :as s]))
[uxbox.queries :as qs]))

;; ================================================================================
;; Queries
Expand Down Expand Up @@ -43,13 +42,14 @@
:will-mount
(fn [state]
(let [[conn] (:rum/args state)
local-state (s/pipe-to-atom (sq/rpull query pull conn))
local-state (qs/rpull query pull conn)
component (:rum/react-component state)]
;; sub
(add-watch local-state
key
(fn [_ _ _ _]
(rum/request-render component)))
(fn [_ _ old new]
(when-not (= old new)
(rum/request-render component))))
(assoc state key local-state)))
:will-unmount
(fn [state]
Expand Down
141 changes: 62 additions & 79 deletions test/uxbox/test/queries_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,126 +9,113 @@
(t/deftest reactive-query
(t/testing "Reactive queries can be constructed"
(let [conn (db/create)
vals (volatile! [])
s (qs/rquery '[:find [?n ...]
a (qs/rquery '[:find [?n ...]
:where
[?e :name ?n]
[?e :cool? true]]
conn)]
(s/on-value s #(vswap! vals conj %))

(d/transact! conn [{:name "John"
:cool? true}])
(t/is (= @a
["John"]))

(d/transact! conn [{:name "Mariano"
:cool? false}])
(t/is (= @a
["John"]))

(d/transact! conn [{:db/id 42
:name "Grace"
:cool? true}
{:name "Ada"
:cool? true}])

(d/transact! conn [[:db/add 42 :cool? false]])

(t/is (= @vals
[[]
(t/is (= @a
["John" "Grace" "Ada"]))

["John"]

["John" "Grace" "Ada"]
(d/transact! conn [[:db/add 42 :cool? false]])

["John" "Ada"]])))))
(t/is (= @a
["John" "Ada"])))))

(t/deftest reactive-pull-query
(t/testing "Reactive pull queries can be constructed"
(let [conn (db/create)
vals (volatile! [])
query '[:find [?e ...]
:where [?e :name ?n]
[?e :cool? true]]
pull '[:name]
s (qs/rpull query pull conn)]
(s/on-value s #(vswap! vals conj %))

a (qs/rpull query pull conn)]
(d/transact! conn [{:name "John"
:cool? true}])
(t/is (= @a
[{:name "John"}]))

(d/transact! conn [{:name "Mariano"
:cool? false}])
(t/is (= @a
[{:name "John"}]))

(d/transact! conn [{:name "Grace"
:cool? true}
{:db/id 42
(d/transact! conn [{:db/id 42
:name "Ada"
:cool? true}])

(d/transact! conn [[:db/add 42 :name "Ada Lovelace"]])

(t/is (= @vals
[[]

[{:name "John"}]

(t/is (= @a
[{:name "John"}
{:name "Grace"}
{:name "Ada"}]
{:name "Ada"}]))

(d/transact! conn [[:db/add 42 :name "Ada Lovelace"]])

(t/is (= @a
[{:name "John"}
{:name "Grace"}
{:name "Ada Lovelace"}]])))))
{:name "Ada Lovelace"}])))))

(t/deftest reactive-entity
(t/testing "Reactive entities can be queried"
(let [conn (db/create)
vals (volatile! [])

id 42
u (random-uuid)
name "UXBox"
width 400
height 500

s (qs/rentity id conn)]

(s/on-value s #(vswap! vals conj %))
a (qs/rentity id conn)]
(t/is (= @a
{:db/id 42}))

(d/transact! conn [[:db/add 42 :project/uuid u]])
(t/is (= @a
{:db/id 42
:project/uuid u
}))

;; note: stream should be deduped
(d/transact! conn [[:db/add 42 :project/name name]])
(d/transact! conn [[:db/add 42 :project/name name]])
(t/is (= @a
{:db/id 42
:project/uuid u
:project/name name}))

(d/transact! conn [[:db/add 42 :project/width width]])
(t/is (= @a
{:db/id 42
:project/uuid u
:project/name name
:project/width width}))

(d/transact! conn [[:db/add 42 :project/name "Neo UXBox"]
[:db/add 42 :project/height height]])

(t/is (= @vals
[{:db/id 42}

{:db/id 42
:project/uuid u}

{:db/id 42
:project/uuid u
:project/name name}

{:db/id 42
:project/uuid u
:project/name name
:project/width width}

{:db/id 42
:project/uuid u
:project/name "Neo UXBox"
:project/width width
:project/height height}])))))
(t/is (= @a
{:db/id 42
:project/uuid u
:project/name "Neo UXBox"
:project/width width
:project/height height})))))

(t/deftest reactive-pull-entity
(t/testing "Reactive entities can be queried with a pull"
(let [conn (db/create)
vals (volatile! [])

id 42
u (random-uuid)
Expand All @@ -141,34 +128,30 @@
:project/width
:project/height]

s (qs/rentity id pull conn)]

(s/on-value s #(vswap! vals conj %))
a (qs/rentity id pull conn)]
(t/is (= @a
nil))

(d/transact! conn [[:db/add 42 :project/uuid u]])
(t/is (= @a
{:project/uuid u
}))

;; note: stream should be deduped
(d/transact! conn [[:db/add 42 :project/name name]])
(d/transact! conn [[:db/add 42 :project/name name]])
(t/is (= @a
{:project/uuid u
:project/name name}))

(d/transact! conn [[:db/add 42 :project/width width]])
(t/is (= @a
{:project/uuid u
:project/name name
:project/width width}))

(d/transact! conn [[:db/add 42 :project/name "Neo UXBox"]
[:db/add 42 :project/height height]])

(t/is (= @vals
[nil

{:project/uuid u}

{:project/uuid u
:project/name name}

{:project/uuid u
:project/name name
:project/width width}

{:project/uuid u
:project/name "Neo UXBox"
:project/width width
:project/height height}])))))
(t/is (= @a
{:project/uuid u
:project/name "Neo UXBox"
:project/width width
:project/height height})))))

0 comments on commit faded28

Please sign in to comment.