Skip to content

Commit

Permalink
Merge pull request #20 from denistakeda/feat/re-frame-reimport
Browse files Browse the repository at this point in the history
feat(app): reimport re-frame and datascript libraries
  • Loading branch information
denistakeda authored Jun 5, 2018
2 parents 9b44125 + b4f4445 commit 7e65d75
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 95 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ pom.xml.asc
/.nrepl-port
.hgignore
.hg/
/examples/todomvc/.cljs_rhino_repl
/examples/todomvc/.nrepl-port
/examples/todomvc/.rebel_readline_history
/examples/todomvc/figwheel_server.log
/examples/todomvc/resources/public/js
/examples/todomvc/target
32 changes: 8 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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]]}))
```
Expand All @@ -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

37 changes: 20 additions & 17 deletions examples/todomvc/project.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
(defproject todomvc "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.9.908"]
[reagent "0.7.0"]
[datascript "0.16.2"]
[re-frame "0.10.1"]
[re-posh "0.1.5"]]
[org.clojure/clojurescript "1.10.238"]
[reagent "0.8.1"]
[re-posh "0.1.6"]]

:plugins [[lein-cljsbuild "1.1.5"]]
:plugins [[lein-cljsbuild "1.1.7"]]

:min-lein-version "2.5.3"

Expand All @@ -16,11 +14,18 @@

:figwheel {:css-dirs ["resources/public/css"]}

:repl-options {:nrepl-middleware [cider.piggieback/wrap-cljs-repl]}

:profiles
{:dev
{:dependencies [[binaryage/devtools "0.9.4"]]
{:dependencies [[binaryage/devtools "0.9.10"]
[day8.re-frame/re-frame-10x "0.3.3"]
[day8.re-frame/tracing "0.5.1"]
[figwheel-sidecar "0.5.16"]
[cider/piggieback "0.3.5"]]

:plugins [[lein-figwheel "0.5.13"]]}}
:plugins [[lein-figwheel "0.5.16"]]}
:prod { :dependencies [[day8.re-frame/tracing-stubs "0.5.1"]]}}

:cljsbuild
{:builds
Expand All @@ -32,19 +37,17 @@
:output-dir "resources/public/js/compiled/out"
:asset-path "js/compiled/out"
:source-map-timestamp true
:preloads [devtools.preload]
:external-config {:devtools/config {:features-to-install :all}}
}}
:preloads [devtools.preload
day8.re-frame-10x.preload]
:closure-defines {"re_frame.trace.trace_enabled_QMARK_" true
"day8.re_frame.tracing.trace_enabled_QMARK_" true}
:external-config {:devtools/config {:features-to-install :all}}}}


{:id "min"
:source-paths ["src/cljs"]
:compiler {:main todomvc.core
:output-to "resources/public/js/compiled/app.js"
:optimizations :advanced
:closure-defines {goog.DEBUG false}
:pretty-print false}}


]}

)
:pretty-print false}}]})
5 changes: 2 additions & 3 deletions examples/todomvc/src/cljs/todomvc/core.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns todomvc.core
(:require [reagent.core :as reagent]
[re-frame.core :as re-frame]
[re-posh.core :as re-posh]
[todomvc.events]
[todomvc.subs]
[todomvc.views :as views]
Expand All @@ -13,11 +13,10 @@
(println "dev mode")))

(defn mount-root []
(re-frame/clear-subscription-cache!)
(reagent/render [views/main-panel]
(.getElementById js/document "app")))

(defn ^:export init []
(re-frame/dispatch-sync [:initialize-db])
(re-posh/dispatch-sync [:initialize-db])
(dev-setup)
(mount-root))
37 changes: 16 additions & 21 deletions examples/todomvc/src/cljs/todomvc/db.cljs
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}])
6 changes: 3 additions & 3 deletions examples/todomvc/src/cljs/todomvc/events.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns todomvc.events
(:require [re-frame.core :as re-frame]
[re-posh.core :as re-posh]
[todomvc.db :as db]))
(:require
[re-posh.core :as re-posh]
[todomvc.db :as db]))

(re-posh/reg-event-ds
:initialize-db
Expand Down
5 changes: 2 additions & 3 deletions examples/todomvc/src/cljs/todomvc/subs.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns todomvc.subs
(:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :as re-frame]
[re-posh.core :as re-posh]))
(:require
[re-posh.core :as re-posh]))

(re-posh/reg-query-sub
:create-todo-form/id
Expand Down
26 changes: 13 additions & 13 deletions examples/todomvc/src/cljs/todomvc/views.cljs
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]])
6 changes: 3 additions & 3 deletions project.clj
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"]])
12 changes: 9 additions & 3 deletions src/re_posh/core.cljc
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)
14 changes: 9 additions & 5 deletions src/re_posh/db.cljc
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!)

0 comments on commit 7e65d75

Please sign in to comment.