Skip to content

Commit

Permalink
Merge pull request #34 from Quezion/pull-many-support
Browse files Browse the repository at this point in the history
Add reg-pull-many-sub {:type :pull-many :ids [...]}
  • Loading branch information
denistakeda committed Oct 24, 2019
2 parents c390b2a + 9657aa6 commit 3aa30c2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You can also leverage [DatSync](https://github.com/metasoarous/datsync) to have
re-frame is a [reactive programming](https://gist.github.com/staltz/868e7e9bc2a7b8c1f754) library for writing single-page apps in ClojureScript
using [Reagent](https://github.com/reagent-project/reagent), which wraps Facebook's popular [React](https://facebook.github.io/react/) library for building component-oriented user interfaces.

[Posh](https://github.com/mpdairy/posh) improves Reagent to allow the declarative binding of user interface components to a local DataScript database. Like Datomic, DataScript supports the powerful `q` and `pull` query API's, and [Datalog](http://www.learndatalogtoday.org/) in general.
[Posh](https://github.com/denistakeda/posh) improves Reagent to allow the declarative binding of user interface components to a local DataScript database. Like Datomic, DataScript supports the powerful `q` and `pull` query API's, and [Datalog](http://www.learndatalogtoday.org/) in general.

`re-posh` allows Posh and re-frame to work together by adding support for re-frame specific `subscriptions`, `events`, `effects`, and `co-effects` to Posh.

Expand Down Expand Up @@ -85,7 +85,7 @@ reg-sub needs three things:
- the required inputs for this node
- a function that generates config for query or pull for this node

The `query-id` is always the 1st argument to reg-sub and it is typicallya namespaced keyword.
The `query-id` is always the 1st argument to reg-sub and it is typically a namespaced keyword.

A config function is always the last argument and it has this general form: `(input-signals, query-vector) -> a-value`

Expand Down Expand Up @@ -198,6 +198,19 @@ Pull subscriptions creates subscription to the entity. `reg-pull-sub` function c
entity (subscribe [:sub-name entity-id])])
```

Pull-many subscriptions are similar to pull but take a vector of entity-ids.

```clojure
(reg-pull-many-sub
:sub-name
'[*])

;; Usage

(let [entity-id 123
entity (subscribe [:sub-name [eids])])
```

### Combining subscriptions

It's often the case that combining of several subscriptions (espetially query and pull subscriptions) required. Unfortunatelly `re-posh` doesn't support combining them inside query. But there is another way. The classical example is when we have a query that returns some object id and we needs the whole object (pull). Here is how can we do that:
Expand All @@ -221,6 +234,27 @@ It's often the case that combining of several subscriptions (espetially query an

In this example two queries are generated. The first one is independent. It returns the id of required object. The second one depends of the first one. It takes the object id as param and returns the whole object.

Another example to show usage of `pull-many` from a query that returns several entity-ids.

```clojure
(reg-sub
:todo-ids
(fn [_ _]
{:type :query
:query '[:find ?id
:where [?id :item/type :type/todo]}))

(reg-sub
:todos
:<- [:todo-ids]
(fn [entity-ids _]
{:type :pull-many
:pattern '[*]
:ids (reduce into [] entity-ids)}))
```

Note the `(reduce ...)` & recall that the query returns its results in form `#{[1] [2] ...}`, but the pull-many sub expects a sequence of entity-ids.

## Events

`re-posh` uses totally the same solution as re-frame `reg-event-db` but with datascript database instead. Function `reg-event-ds` takes event name and event handler. First param for handler is a dereferenced DataScript database. You can do with it whatewer you like, make query or take entities with pull. The second parameter is a signal. Event handler have to return transaction.
Expand Down Expand Up @@ -276,6 +310,6 @@ Pull requests are welcome. Email me on <denis.takeda@gmail.com> if you have any

## License

Copyright © 2018 Denis Krivosheev
Copyright © 2019 Denis Krivosheev

Distributed under the MIT License
8 changes: 4 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(defproject re-posh "0.3.0"
(defproject re-posh "0.3.1"
:description "Use your re-frame with DataScript as a data storage"
:url "https://github.com/denistakeda/re-posh"
:license {:name "MIT"
:url "https://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.9.0"]
[datascript "0.16.6"]
[re-frame "0.10.5"]
[posh "0.5.5"]])
[datascript "0.18.7"]
[re-frame "0.10.7"]
[denistakeda/posh "0.5.7"]])
1 change: 1 addition & 0 deletions src/re_posh/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

(def reg-query-sub subs/reg-query-sub)
(def reg-pull-sub subs/reg-pull-sub)
(def reg-pull-many-sub subs/reg-pull-many-sub)
(def reg-sub subs/reg-sub)
(def reg-event-ds events/reg-event-ds)
(def connect! db/connect!)
Expand Down
25 changes: 25 additions & 0 deletions src/re_posh/subs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
[{:keys [pattern id]}]
(p/pull @store pattern id))

(defmethod execute-sub :pull-many
[{:keys [pattern ids]}]
(p/pull-many @store pattern ids))

(defn reg-sub
"For a given `query-id` register a `config` function and input `signals`
Expand Down Expand Up @@ -183,3 +187,24 @@
{:type :pull
:pattern pattern
:id id})))

(defn reg-pull-many-sub
"Syntax sugar for writing pull-many queries.
Same as reg-pull-sub but takes vector of eids under key :ids
(reg-pull-many-sub
:things
'[*])
It's possible to subscribe to this pull-many query with
(re-posh/subscribe [:things ids])
Where ids is a sequence of entity ids"
[sub-name pattern]
(reg-sub
sub-name
(fn [_ [_ ids]]
{:type :pull-many
:pattern pattern
:ids ids})))

0 comments on commit 3aa30c2

Please sign in to comment.