From d1d14bc710a61c0a4c181ff5509232c73c2f304b Mon Sep 17 00:00:00 2001 From: anatoly Date: Mon, 7 Dec 2015 13:16:53 -0500 Subject: [PATCH] #10: adding datascript to the cljs example app --- .gitignore | 3 +-- .../public/index.html | 0 project.clj | 14 +++++------ src/mount/core.cljc | 3 +-- test/mount/example/app.cljs | 16 +++++++++++++ test/mount/example/app_config.cljs | 3 ++- test/mount/example/audit_log.cljs | 24 +++++++++++++++++++ test/mount/example/cljs.cljs | 13 ---------- test/mount/example/websockets.cljs | 11 +++++---- 9 files changed, 57 insertions(+), 30 deletions(-) rename {test/resources => dev-resources}/public/index.html (100%) create mode 100644 test/mount/example/app.cljs create mode 100644 test/mount/example/audit_log.cljs delete mode 100644 test/mount/example/cljs.cljs diff --git a/.gitignore b/.gitignore index 5c17b56..f7c8d0f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,7 @@ pom.xml pom.xml.asc .repl* -/dev-resources -/test/resources/public/js +/dev-resources/resources/public/js/* figwheel_server.log *.jar *.class diff --git a/test/resources/public/index.html b/dev-resources/public/index.html similarity index 100% rename from test/resources/public/index.html rename to dev-resources/public/index.html diff --git a/project.clj b/project.clj index e92ffeb..100c6a9 100644 --- a/project.clj +++ b/project.clj @@ -10,7 +10,8 @@ [org.clojure/clojurescript "1.7.170"]] :profiles {:dev {:source-paths ["dev" "test/app"] - :dependencies [[ch.qos.logback/logback-classic "1.1.3"] + :dependencies [[datascript "0.13.3"] + [ch.qos.logback/logback-classic "1.1.3"] [org.clojure/tools.logging "0.3.1"] [robert/hooke "1.3.0"] [org.clojure/tools.namespace "0.2.11"] @@ -28,16 +29,15 @@ {:source-paths ["src" "test"] :figwheel true - :compiler {:main mount.example.cljs + :compiler {:main mount.example.app :asset-path "js/compiled/out" - :output-to "test/resources/public/js/compiled/mount.js" - :output-dir "test/resources/public/js/compiled/out" + :output-to "dev-resources/public/js/compiled/mount.js" + :output-dir "dev-resources/public/js/compiled/out" :optimizations :none :source-map true - :source-map-timestamp true - :cache-analysis true}} + :source-map-timestamp true}} :prod {:source-paths ["src" "test"] - :compiler {:output-to "test/resources/public/js/compiled/mount.js" + :compiler {:output-to "dev-resources/public/js/compiled/mount.js" :optimizations :advanced :pretty-print false}}}}}}) diff --git a/src/mount/core.cljc b/src/mount/core.cljc index 4e60235..9e7994c 100644 --- a/src/mount/core.cljc +++ b/src/mount/core.cljc @@ -1,5 +1,4 @@ -(ns #?(:clj mount.core - :cljs ^:figwheel-no-load mount.core) +(ns mount.core #?(:clj (:require [mount.tools.macro :refer [on-error throw-runtime] :as macro]) :cljs (:require [mount.tools.macro :as macro] [mount.tools.cljs :as cljs])) diff --git a/test/mount/example/app.cljs b/test/mount/example/app.cljs new file mode 100644 index 0000000..e0027a8 --- /dev/null +++ b/test/mount/example/app.cljs @@ -0,0 +1,16 @@ +(ns mount.example.app + (:require [mount.core :as mount] + [mount.example.app-config] + [mount.example.websockets] + [mount.example.audit-log :refer [log find-all-logs]])) + +(defn show-log [] + (.write js/document + (interpose "
" (find-all-logs log)))) + +(mount/start) + +;; time for websocket to connect +(js/setTimeout #(do (mount/stop-except "#'mount.example.audit-log/log") + (show-log)) 500) + diff --git a/test/mount/example/app_config.cljs b/test/mount/example/app_config.cljs index 4952209..c3687fc 100644 --- a/test/mount/example/app_config.cljs +++ b/test/mount/example/app_config.cljs @@ -1,8 +1,9 @@ (ns mount.example.app-config + (:require [mount.example.audit-log :refer [audit log]]) (:require-macros [mount.core :refer [defstate]])) (defn load-config [path] - (println "loading config from " path " (at least pretending)") + (audit log :app-config "loading config from '" path "' (at least pretending)") {:system-a {:uri "ws://echo.websocket.org/"}}) (defstate config :start (load-config "resources/config.end")) diff --git a/test/mount/example/audit_log.cljs b/test/mount/example/audit_log.cljs new file mode 100644 index 0000000..1e29ee7 --- /dev/null +++ b/test/mount/example/audit_log.cljs @@ -0,0 +1,24 @@ +(ns mount.example.audit-log + (:require [datascript.core :as d]) + (:require-macros [mount.core :refer [defstate]])) + +(defstate log :start (d/create-conn {})) + +(defn audit [db source & msg] + (d/transact! @db [{:db/id -1 + :source source + :timestamp (js/Date.) + :msg (apply str msg)}])) + +(defn find-source-logs [db source] + (d/q '{:find [?t ?msg] + :in [$ ?s] + :where [[?e :source ?s] + [?e :timestamp ?t] + [?e :msg ?msg]]} + @@db source)) + +(defn find-all-logs [db] + (->> (map :e (d/datoms @@db :aevt :timestamp)) + dedupe + (d/pull-many @@db '[:timestamp :source :msg]))) diff --git a/test/mount/example/cljs.cljs b/test/mount/example/cljs.cljs deleted file mode 100644 index a7d800f..0000000 --- a/test/mount/example/cljs.cljs +++ /dev/null @@ -1,13 +0,0 @@ -(ns mount.example.cljs - (:require [mount.core :as mount] - [mount.example.websockets :refer [system-a]])) - -(enable-console-print!) - -(println "(mount/start)" (mount/start)) - -(println "system-a: " @system-a) - -;; time for websocket to connect -(js/setTimeout #(println "(mount/stop)" (mount/stop)) - 500) diff --git a/test/mount/example/websockets.cljs b/test/mount/example/websockets.cljs index 068a492..df90485 100644 --- a/test/mount/example/websockets.cljs +++ b/test/mount/example/websockets.cljs @@ -1,16 +1,17 @@ (ns mount.example.websockets - (:require [mount.example.app-config :refer [config]]) + (:require [mount.example.app-config :refer [config]] + [mount.example.audit-log :refer [audit log]]) (:require-macros [mount.core :refer [defstate]])) (defn connect [uri] - (println "connecting to " uri) + (audit log :system-a "connecting to '" uri "'") (let [ws (js/WebSocket. uri)] - (set! (.-onopen ws) #(println "opening ws @" uri)) - (set! (.-onclose ws) #(println "closing ws @" uri)) + (set! (.-onopen ws) #(audit log :system-a "opening ws @" uri)) + (set! (.-onclose ws) #(audit log :system-a "closing ws @" uri)) ws)) (defn disconnect [ws] - (println "disconnecting " @ws) + (audit log "disconnecting " @ws) (.close @ws)) (defstate system-a :start (connect (get-in @config [:system-a :uri]))