Skip to content

Commit

Permalink
dont assume connection handling inside rata
Browse files Browse the repository at this point in the history
made q! and pull! depend only on arguments not on global state
  • Loading branch information
carocad committed Oct 7, 2018
1 parent b6057c1 commit 4c95bb4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pom.xml.asc
.hgignore
.hg/
/rata.iml
/.cljs_rhino_repl/
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@

Reactive [Datascript](https://github.com/tonsky/datascript/) queries through [Reagent's](https://github.com/reagent-project/reagent) track mechanism

## Example
## usage

Just use reagent as you normally would. The only difference is in
how you fetch the data. Use Datascript queries and pull patterns :)
Rata hooks itself into the transactor of Datascript. So you just need to register
it against Datascript's connection. From that point onwards, you should use
`rata/q` and `rata/pull` with the **connection**

```clojure
(ns example.core
(:require [reagent.core :as reagent]
[datascript.core :as data]
[hiposfer.rata.core :as rata]))

;; WARNING: dont do this at home
(defonce foo (rata/init! (data/create-conn {:user/input {:db.unique :db.unique/identity}})))
(defonce state (data/create-conn {:user/input {:db.unique :db.unique/identity}}))

(rata/listen! state)

(defn hello-world
[]
(let [click-count @(rata/q! '[:find ?count .
:where [?input :user/input "click"]
[?input :click/count ?count]])]
[?input :click/count ?count]]
state)] ;; this is conn not the db as in datascript !!
[:div "For each click, you get a greeting :)"
[:input {:type "button" :value "Click me!"
:on-click #(rata/transact! [{:user/input "click"
:click/count (inc click-count)}])}]
:on-click #(data/transact! state [{:user/input "click"
:click/count (inc click-count)}])}]
(for [i (range click-count)]
^{:key i}
[:div "hello " i])]))


(reagent/render-component [hello-world]
(. js/document (getElementById "app")))
```
Expand Down
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject hiposfer/rata "0.1.1"
(defproject hiposfer/rata "0.2.0"
:description "Reactive Datascript queries"
:url "https://github.com/hiposfer/hiposfer.rata"
:license {:name "LGPLv3"
Expand All @@ -8,4 +8,5 @@
[reagent "0.7.0" :scope "provided"]
[datascript "0.16.6"]]
;; deploy to clojars as - lein deploy releases
:deploy-repositories [["releases" {:sign-releases false :url "https://clojars.org/repo"}]])
:deploy-repositories [["releases" {:sign-releases false :url "https://clojars.org/repo"}]
["snapshots" {:sign-releases false :url "https://clojars.org/repo"}]])
61 changes: 20 additions & 41 deletions src/hiposfer/rata/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@
See https://reagent-project.github.io/news/news060-alpha.html
for more information"
(:require [datascript.core :as data]
[reagent.core :as r]
[hiposfer.rata.state :as state]))
[reagent.core :as r]))

(defn- listen!
"registers a listener for the connection transactions. Returns a
reagent/ratom whose value will automatically updated on every
transact"
(defn listen!
"registers a listener for the connection transactions. Returns
the conn object itself with the added listener and state holder
Subsequent usages of conn in q! and pull! will return reactive
atoms that will update their value whenever the Datascript value
changes"
[conn]
(let [ratom (r/atom @conn)]
(data/listen! conn ::tx (fn [tx-report] (reset! ratom (:db-after tx-report))))
(swap! conn assoc ::ratom ratom)))
(when (nil? (::ratom @conn))
(let [ratom (r/atom @conn)] ;; initial state
(data/listen! conn ::tx (fn [tx-report] (reset! ratom (:db-after tx-report))))
;; keep a reference to the ratom to avoid GC
(swap! conn assoc ::ratom ratom)))
;; return the conn again to allow standard datascript usage
conn)

(defn- unlisten!
(defn unlisten!
"unregisters the transaction listener previously attached with
listen!"
[conn]
Expand All @@ -52,42 +58,15 @@
[ratom selector eid]
(data/pull @ratom selector eid))

(defn init!
"takes a Datascript conn and starts listening to its transactor for changes"
[conn]
(when (some? state/conn) ;; just in case
(unlisten! state/conn)
(unlisten! conn))
(set! state/conn conn)
(listen! state/conn))

(defn pull!
"same as datascript/pull but returns a ratom which will be updated
every time that the value of conn changes"
[selector eid]
(r/track! pull* (::ratom @state/conn) selector eid))
[conn selector eid]
(r/track! pull* (::ratom @conn) selector eid))

(defn q!
"Returns a reagent/atom with the result of the query.
The value of the ratom will be automatically updated whenever
a change is detected"
[query & inputs]
(r/track! q* query (::ratom @state/conn) inputs))

(defn db
"return the Datascript Database instance.
The returned version is immutable, therefore you cannot use
datascript/transact!.
This is meant to keep querying separate from mutations"
[]
@state/conn)

(defn transact!
"same as Datascript transact except that uses the connection
passed at init!"
([tx-data]
(transact! tx-data nil))
([tx-data tx-meta]
(data/transact! state/conn tx-data tx-meta)))
[query conn & inputs]
(r/track! q* query (::ratom @conn) inputs))
7 changes: 0 additions & 7 deletions src/hiposfer/rata/state.cljs

This file was deleted.

0 comments on commit 4c95bb4

Please sign in to comment.