-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from denistakeda/feat/re-frame-reimport
feat(app): reimport re-frame and datascript libraries
- Loading branch information
Showing
11 changed files
with
91 additions
and
95 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,29 +25,14 @@ State management within *any* application, if treated as a secondary concern, ca | |
Start a re-frame project and include this dependency: | ||
|
||
```clj | ||
[re-posh "0.1.5"] | ||
[re-posh "0.1.6"] | ||
``` | ||
|
||
Require `re-posh` in your app: | ||
```clojure | ||
(ns example | ||
(:require [reagent.core :as r] | ||
[re-posh.core :refer [connect! reg-query-sub reg-pull-sub reg-event-ds]] | ||
[datascript.core :as d])) | ||
``` | ||
|
||
## Connection | ||
|
||
Connect your DataScript database to `re-posh`: | ||
|
||
```clojure | ||
(ns example.db | ||
(:require | ||
[datascript.core :as d] | ||
[re-posh.core :refer [connect!]])) | ||
|
||
(def conn (d/create-conn)) | ||
(connect! conn) | ||
[re-posh.core :refer [reg-query-sub reg-pull-sub reg-event-ds subscribe dispatch]])) | ||
``` | ||
|
||
## Subscriptions | ||
|
@@ -124,9 +109,9 @@ This effect commit transaction into the DataScript database | |
|
||
```clojure | ||
(ns example.events | ||
(:require [re-frame.core :as r])) | ||
(:require [re-posh.core :as re-posh])) | ||
|
||
(r/reg-event-fx | ||
(re-posh/reg-event-fx | ||
:my-event | ||
(fn [cofx [_ id k v]] | ||
{:transact [[:db/add id k v]]})) ;; return datascript transaction | ||
|
@@ -138,11 +123,11 @@ This co-effect provide DataScript database into your event handler | |
|
||
```clojure | ||
(ns example.events | ||
(:require [re-frame.core :as r])) | ||
(:require [re-posh.core :as re-posh])) | ||
|
||
(r/reg-event-fx | ||
(re-posh/reg-event-fx | ||
:my-event | ||
[(r/inject-cofx :ds)] ;; inject coeffect | ||
[(re-posh/inject-cofx :ds)] ;; inject coeffect | ||
(fn [{:keys [ds]} [_ id k v]] ;; ds here is the DataScript database | ||
{:transact [[:db/add id k v]]})) | ||
``` | ||
|
@@ -157,7 +142,6 @@ Pull requests are welcome. Email me on <[email protected]> if you have any | |
|
||
## License | ||
|
||
Copyright © 2017 Denis Krivosheev | ||
Copyright © 2018 Denis Krivosheev | ||
|
||
Distributed under the MIT License | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
(ns todomvc.db | ||
(:require | ||
[datascript.core :as d] | ||
[re-posh.core :refer [connect!]])) | ||
(ns todomvc.db) | ||
|
||
(def initial-db [{ :db/id -1 | ||
:app/type :type/create-todo-form | ||
:create-todo-form/title "" | ||
:create-todo-form/description "" } | ||
{ :db/id -2 | ||
:app/type :type/task | ||
:task/title "Learn Clojure a little bit" | ||
:task/description "Just learn it" | ||
:task/done? false } | ||
{ :db/id -3 | ||
:app/type :type/task | ||
:task/title "Have a coffe" | ||
:task/description "Just relax" | ||
:task/done? false } ]) | ||
|
||
(def conn (d/create-conn)) | ||
(connect! conn) | ||
(def initial-db | ||
[{:db/id -1 | ||
:app/type :type/create-todo-form | ||
:create-todo-form/title "" | ||
:create-todo-form/description ""} | ||
{:db/id -2 | ||
:app/type :type/task | ||
:task/title "Learn Clojure a little bit" | ||
:task/description "Just learn it" | ||
:task/done? false} | ||
{:db/id -3 | ||
:app/type :type/task | ||
:task/title "Have a coffe" | ||
:task/description "Just relax" | ||
:task/done? false}]) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
(ns todomvc.views | ||
(:require [re-frame.core :as re-frame])) | ||
(:require [re-posh.core :refer [subscribe dispatch]])) | ||
|
||
(defn create-task-panel [] | ||
(let [form-id (re-frame/subscribe [:create-todo-form/id]) | ||
form (re-frame/subscribe [:create-todo-form @form-id])] | ||
(let [form-id (subscribe [:create-todo-form/id]) | ||
form (subscribe [:create-todo-form @form-id])] | ||
(fn [] | ||
[:div {:class-name "create-task-panel"} | ||
[:div.create-tast-panel | ||
[:input {:type "text" | ||
:value (:create-todo-form/title @form) | ||
:on-change #(re-frame/dispatch [:create-todo-form/set-title @form-id (-> % .-target .-value)])}] | ||
[:button {:class-name "create-task-button" | ||
:on-click #(re-frame/dispatch [:create-todo-form/create-todo @form-id (:create-todo-form/title @form)])} "Create"]]))) | ||
:on-change #(dispatch [:create-todo-form/set-title @form-id (-> % .-target .-value)])}] | ||
[:button.create-task-button | ||
{:on-click #(dispatch [:create-todo-form/create-todo @form-id (:create-todo-form/title @form)])} "Create"]]))) | ||
|
||
(defn task-list-item [id] | ||
(let [task (re-frame/subscribe [:task id])] | ||
(let [task (subscribe [:task id])] | ||
(fn [] | ||
[:div {:class-name "task-list-item"} | ||
[:div.task-list-item | ||
[:input {:type "checkbox" | ||
:checked (:task/done? @task) | ||
:on-change #(re-frame/dispatch [:task/set-status (:db/id @task) (not (:task/done? @task))])}] | ||
:on-change #(dispatch [:task/set-status (:db/id @task) (not (:task/done? @task))])}] | ||
[:span (:task/title @task)]]))) | ||
|
||
(defn task-list [] | ||
(let [task-ids (re-frame/subscribe [:task-ids])] | ||
(let [task-ids (subscribe [:task-ids])] | ||
(fn [] | ||
[:div {:class-name "task-list"} | ||
[:div.task-list | ||
(for [task-id @task-ids] | ||
^{:key task-id} [task-list-item task-id])]))) | ||
|
||
(defn main-panel [] | ||
[:div {:class-name "main-panel"} | ||
[:div.main-panel | ||
[:h1 "TodoMVC"] | ||
[create-task-panel] | ||
[task-list]]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
(defproject re-posh "0.1.5" | ||
(defproject re-posh "0.1.6" | ||
: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.8.0"] | ||
[re-frame "0.9.4"] | ||
:dependencies [[org.clojure/clojure "1.9.0"] | ||
[re-frame "0.10.5"] | ||
[posh "0.5.5"]]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
(ns re-posh.core | ||
(:require | ||
[re-posh.db :as db] | ||
[re-posh.subs :as subs] | ||
[re-posh.events :as events] | ||
[re-posh.effects] | ||
[re-posh.coeffects])) | ||
[re-posh.coeffects] | ||
[re-frame.core :as re-frame])) | ||
|
||
(def connect! db/connect!) | ||
(def reg-query-sub subs/reg-query-sub) | ||
(def reg-pull-sub subs/reg-pull-sub) | ||
(def reg-event-ds events/reg-event-ds) | ||
|
||
;; Reexport re-frame functions | ||
(def subscribe re-frame/subscribe) | ||
(def dispatch re-frame/dispatch) | ||
(def dispatch-sync re-frame/dispatch-sync) | ||
(def reg-event-fx re-frame/reg-event-fx) | ||
(def inject-cofx re-frame/inject-cofx) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
(ns re-posh.db | ||
(:require [posh.reagent :as p])) | ||
(:require | ||
[posh.reagent :as p] | ||
[datascript.core :as datascript])) | ||
|
||
;; Basic store. This atom stores another atom | ||
;; @store - datascript connection | ||
;; @@store - datascript database | ||
(def store (atom nil)) | ||
|
||
(defn connect! | ||
(defn- connect! [] | ||
"Connect DataScript store to the re-frame event system" | ||
[conn] | ||
(p/posh! conn) | ||
(reset! store conn)) | ||
(let [conn (datascript/create-conn)] | ||
(p/posh! conn) | ||
(reset! store conn))) | ||
|
||
(connect!) |