From b4f44457462c6748265a030705139a547f575271 Mon Sep 17 00:00:00 2001 From: Denis Krivosheev Date: Tue, 5 Jun 2018 15:06:47 +0300 Subject: [PATCH] feat(app): reimport re-frame and datascript libraries Reimport basic `datascript` and `re-frame` functions so user don't need to require them explicitly --- .gitignore | 6 +++ README.md | 32 ++++------------ examples/todomvc/project.clj | 37 ++++++++++--------- examples/todomvc/src/cljs/todomvc/core.cljs | 5 +-- examples/todomvc/src/cljs/todomvc/db.cljs | 37 ++++++++----------- examples/todomvc/src/cljs/todomvc/events.cljs | 6 +-- examples/todomvc/src/cljs/todomvc/subs.cljs | 5 +-- examples/todomvc/src/cljs/todomvc/views.cljs | 26 ++++++------- project.clj | 6 +-- src/re_posh/core.cljc | 12 ++++-- src/re_posh/db.cljc | 14 ++++--- 11 files changed, 91 insertions(+), 95 deletions(-) diff --git a/.gitignore b/.gitignore index c53038e..7cc60aa 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index c7871f6..0310460 100644 --- a/README.md +++ b/README.md @@ -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 if you have any ## License - Copyright © 2017 Denis Krivosheev + Copyright © 2018 Denis Krivosheev Distributed under the MIT License - diff --git a/examples/todomvc/project.clj b/examples/todomvc/project.clj index 5591b52..c3d3935 100644 --- a/examples/todomvc/project.clj +++ b/examples/todomvc/project.clj @@ -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" @@ -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 @@ -32,9 +37,12 @@ :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"] @@ -42,9 +50,4 @@ :output-to "resources/public/js/compiled/app.js" :optimizations :advanced :closure-defines {goog.DEBUG false} - :pretty-print false}} - - - ]} - - ) + :pretty-print false}}]}) diff --git a/examples/todomvc/src/cljs/todomvc/core.cljs b/examples/todomvc/src/cljs/todomvc/core.cljs index 09ad5cf..0d4cb90 100644 --- a/examples/todomvc/src/cljs/todomvc/core.cljs +++ b/examples/todomvc/src/cljs/todomvc/core.cljs @@ -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] @@ -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)) diff --git a/examples/todomvc/src/cljs/todomvc/db.cljs b/examples/todomvc/src/cljs/todomvc/db.cljs index 9ae3821..0c92f45 100644 --- a/examples/todomvc/src/cljs/todomvc/db.cljs +++ b/examples/todomvc/src/cljs/todomvc/db.cljs @@ -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}]) diff --git a/examples/todomvc/src/cljs/todomvc/events.cljs b/examples/todomvc/src/cljs/todomvc/events.cljs index 1bc0b02..150aaa7 100644 --- a/examples/todomvc/src/cljs/todomvc/events.cljs +++ b/examples/todomvc/src/cljs/todomvc/events.cljs @@ -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 diff --git a/examples/todomvc/src/cljs/todomvc/subs.cljs b/examples/todomvc/src/cljs/todomvc/subs.cljs index 9fb48d5..65775b0 100644 --- a/examples/todomvc/src/cljs/todomvc/subs.cljs +++ b/examples/todomvc/src/cljs/todomvc/subs.cljs @@ -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 diff --git a/examples/todomvc/src/cljs/todomvc/views.cljs b/examples/todomvc/src/cljs/todomvc/views.cljs index 6e7bcde..3338b3b 100644 --- a/examples/todomvc/src/cljs/todomvc/views.cljs +++ b/examples/todomvc/src/cljs/todomvc/views.cljs @@ -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]]) diff --git a/project.clj b/project.clj index 0439b58..4460fde 100644 --- a/project.clj +++ b/project.clj @@ -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"]]) diff --git a/src/re_posh/core.cljc b/src/re_posh/core.cljc index 0d3272f..78439da 100644 --- a/src/re_posh/core.cljc +++ b/src/re_posh/core.cljc @@ -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) diff --git a/src/re_posh/db.cljc b/src/re_posh/db.cljc index 373366c..c68c5ae 100644 --- a/src/re_posh/db.cljc +++ b/src/re_posh/db.cljc @@ -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!)