From 64b1bb05a78a6b2120a27143aef9f7682e22b5d2 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Sun, 12 Oct 2014 22:57:00 +0300 Subject: [PATCH] Start client code like server side --- dev/user.clj | 2 +- src/clj/grub/core.clj | 36 ++++++++++++++++++++++-------------- src/cljs/grub/core.cljs | 35 ++++++++++++++++++++++++++++------- src/cljs/grub/view/app.cljs | 3 ++- src/cljs/grub/websocket.cljs | 25 ++++++++++++++----------- src/cljx/grub/state.cljx | 5 ++++- 6 files changed, 71 insertions(+), 35 deletions(-) diff --git a/dev/user.clj b/dev/user.clj index c18cd60..5cf5bea 100644 --- a/dev/user.clj +++ b/dev/user.clj @@ -12,7 +12,7 @@ (defn start "Starts the current development system." [] - (alter-var-root #'system system/start system/dev-config)) + (alter-var-root #'system system/start system/dev-system)) (defn stop "Shuts down and destroys the current development system." diff --git a/src/clj/grub/core.clj b/src/clj/grub/core.clj index 9838990..3cc18bb 100644 --- a/src/clj/grub/core.clj +++ b/src/clj/grub/core.clj @@ -38,15 +38,23 @@ (include-js "/js/grub.js") [:script {:type "text/javascript"} "goog.require(\"grub.core\")"]])) -(def prod-config +(def prod-system {:index prod-index-page - :db {:name "grub"} - :port 3000}) + :db {:name "grub" + :db nil + :conn nil} + :port 3000 + :stop-server nil + :states nil}) -(def dev-config +(def dev-system {:index dev-index-page - :db {:name "grub-dev"} - :port 3000}) + :db {:name "grub-dev" + :db nil + :conn nil} + :port 3000 + :stop-server nil + :states nil}) (defn handle-websocket [handler states] (fn [{:keys [websocket?] :as request}] @@ -71,14 +79,14 @@ (handle-root index) (handle-websocket states))) -(defn start [current {:keys [port db] :as config}] +(defn start [current {:keys [port db] :as system}] (let [to-db (chan) db (db/connect-and-handle-events to-db (:name db)) states (state/init-server to-db (db/get-current-state (:db db))) - stop-server (httpkit/run-server (make-handler config states) {:port port})] + stop-server (httpkit/run-server (make-handler system states) {:port port})] (println "Started server on localhost:" port) - (assoc config - :db (merge (:db config) db) + (assoc system + :db (merge (:db system) db) :stop-server stop-server :states states))) @@ -121,8 +129,8 @@ (not= (count arguments) 1) (exit 1 (usage summary)) errors (exit 1 (error-msg errors))) (case (first arguments) - "development" (start (merge dev-config options)) - "dev" (start (merge dev-config options)) - "production" (start (merge prod-config options)) - "prod" (start (merge prod-config options)) + "development" (start (merge dev-system options)) + "dev" (start (merge dev-system options)) + "production" (start (merge prod-system options)) + "prod" (start (merge prod-system options)) (exit 1 (usage summary))))) diff --git a/src/cljs/grub/core.cljs b/src/cljs/grub/core.cljs index a4e9855..cd33cd6 100644 --- a/src/cljs/grub/core.cljs +++ b/src/cljs/grub/core.cljs @@ -1,17 +1,38 @@ (ns grub.core (:require [grub.state :as state] - [grub.websocket :as ws] + [grub.websocket :as websocket] [grub.view.app :as view] [cljs.core.async :as a :refer [! chan]]) (:require-macros [grub.macros :refer [log logs]])) -(defn init-app [] +(def system + {:pending-msg (atom nil) + :ws (atom nil) + :channels {:local-states (chan) + :remote-states (chan) + :to-remote (chan) + :from-remote (chan)} + :view-state nil}) + +(defn start [system] (let [local-states (chan) remote-states (chan) to-remote (chan) - from-remote (chan)] - (view/render-app state/empty-state remote-states local-states) - (ws/connect-client! to-remote from-remote) - (state/init-client from-remote to-remote local-states remote-states))) + from-remote (chan) + view-state (view/render-app state/empty-state remote-states local-states) + ws (websocket/connect (:pending-msg system) to-remote from-remote) + agent-states (state/init-client from-remote to-remote local-states remote-states)] + (assoc system + :ws ws + :channels {:local-states local-states + :remote-states remote-states + :to-remote to-remote + :from-remote from-remote} + :view-state view-state + :agent-states agent-states))) -(init-app) +(defn stop [{:keys [channels ws]} system] + (doseq [c (vals channels)] (a/close! c)) + (websocket/disconnect ws)) + +(start system) diff --git a/src/cljs/grub/view/app.cljs b/src/cljs/grub/view/app.cljs index 167612b..59b67db 100644 --- a/src/cljs/grub/view/app.cljs +++ b/src/cljs/grub/view/app.cljs @@ -40,4 +40,5 @@ (when (= tag :local) (put! >remote new-state)))}) (go (loop [] (when-let [new-state (view new-state)))) (a/pipe remote states) - (a/put! >remote message/full-sync-request))) + (a/put! >remote message/full-sync-request) + states))