Browse Source

updated web client to be standalone, server module can depend upon it

Harlan Iverson 9 năm trước cách đây
mục cha
commit
08a3763283

+ 23 - 0
web-client/dev-src/example/dev.cljs

@@ -0,0 +1,23 @@
+(ns example.dev
+  (:require [example.core :as core]
+            [com.stuartsierra.component :as component]))
+
+(defonce _ (.warn js/console "Dev mode loaded -- hack responsibly."))
+
+(defrecord DevMode []
+           component/Lifecycle
+           (start [component]
+                  (.info js/console "Dev mode on. :)")
+                  component)
+           (stop [component]
+                 (.info js/console "Dev mode off.")
+                 component))
+
+(swap! core/system update :dev #(or % (component/using (map->DevMode {})
+                                                       {:app :app})))
+
+(defn reload! [& _]
+      (.info js/console (str "Dev Reload. system=" @core/system))
+      (swap! core/system #(-> %
+                              component/stop
+                              component/start)))

+ 38 - 24
web-client/project.clj

@@ -1,45 +1,59 @@
 (defproject com.analogzen.clojure-stack/web-client "0.0.1-SNAPSHOT"
             :dependencies [[org.clojure/clojure :clj1.8]
                            [org.clojure/clojurescript :cljs1.8]
-                           [reagent "0.5.1"
-                            :exclusions [org.clojure/tools.reader]]]
+                           [com.stuartsierra/component :component/clojure1.7+]
+                           ;[reagent "0.5.1"
+                           ; :exclusions [org.clojure/tools.reader]]
+                           ]
 
             :plugins [[lein-modules "0.3.11"]
-                      [lein-figwheel "0.5.0-4"]
-                      [lein-environ "1.0.2"]]
+                      [lein-environ "1.0.2"]
+                      [lein-cljsbuild "1.1.2"]]
 
-            ; opinion: two options: 1) start with figwheel for dev. 2) start with main entry point.
+            ;
 
-            :clean-targets ^{:protect false} [:target-path "resources/public/gen"]
+            :hooks [leiningen.cljsbuild]
 
-            ; opinion: dev should be a layer on top of prod. system-map can be updated by dev accordingly, before (start)
 
-            :cljsbuild {:builds [{:id "dev"
+            ; :prep-tasks ["compile" ["cljsbuild" "once"]]
 
-                                  :source-paths ["src"]
+            :cljsbuild  {:builds {:prod {:source-paths ["src"]
+                                  :compiler     {:main       "example.core"
+                                                  :output-to  "resources/public/gen/js/main.js"
+                                                  :output-dir "resources/public/gen/js"
+                                                  :asset-path "gen/js"}}}}
 
-                                  ; it's nice to show all the options. editor option could be a projection of defaults,
-                                  ; from getting effective project model.
 
-                                  :figwheel {:server-ip "127.0.0.1"
-                                             :server-port 3449
-                                             :server-logfile "figwheel_server.log"
-                                             :http-server-root "public"
+            ; opinion: two options: 1) start with figwheel for dev. 2) start with main entry point.
 
+            :clean-targets ^{:protect false} [:target-path "resources/public/gen"]
 
-                                             :heads-up-display true
-                                             :repl true
-                                             :nrepl-port 7888
-                                             :css-dirs ["resources/public/css"]
-                                             }
-                                  :compiler {:main "example.core"
-                                             :output-to "resources/public/gen/js/main.js"
-                                             :output-dir "resources/public/gen/js"
-                                             :asset-path "gen/js"}}]}
+            ; opinion: dev should be a layer on top of prod. system-map can be updated by dev accordingly, before (start)
+            :profiles {:dev {:plugins [[lein-figwheel "0.5.0-4"]]
+                             :cljsbuild {:builds {:dev {:source-paths ["src" "dev-src"]
 
+                                                        ; if only we could copy these from core...
+                                                        :compiler {:main "example.dev"
+                                                                   :output-to "resources/public/gen/js/main.js"
+                                                                   :output-dir "resources/public/gen/js"
+                                                                   :asset-path "gen/js"}
 
 
 
+                                                        ; it's nice to show all the options. editor option could be a projection of defaults,
+                                                        ; from getting effective project model.
 
 
+                                                        :figwheel {:server-ip "127.0.0.1"
+                                                                   :server-port 3449
+                                                                   :server-logfile "figwheel_server.log"
+                                                                   :http-server-root "public"
+                                                                   :heads-up-display true
+                                                                   :repl true
+                                                                   :nrepl-port 7888
+                                                                   :css-dirs ["resources/public/css"]
+                                                                   :on-jsload example.dev/reload!
+                                                                   ;;; :ring-init example.dev/init! -- no such option
+                                                                   ;:ring-handler example.dev/app
+                                                                   }}}}}}
             :min-lein-version "2.5.0")

+ 1 - 0
web-client/resources/public/index.html

@@ -3,5 +3,6 @@
 <head></head>
 <body>
 <script src="gen/js/main.js" type="text/javascript"></script>
+<script type="text/javascript">example.core.start_BANG_(window);</script>
 </body>
 </html>

+ 17 - 2
web-client/src/example/core.cljs

@@ -1,5 +1,20 @@
-(ns example.core)
+(ns example.core
+  (:require [com.stuartsierra.component :as component]))
 
-(.log js/console "Hey Seymore, sup?")
+(.info js/console "Prod loaded.")
 
+(defrecord App []
+  component/Lifecycle
+  (start [component]
+    (.info js/console "App started.")
+    component)
+  (stop [component]
+    (.info js/console "App stopped.")
+    component))
 
+(defonce system (atom (component/system-map :app (map->App {}))))
+
+(defn start! [window]
+  (.info js/console "Starting...")
+  (swap! system component/start)
+  (.info js.console "Finished starting."))