Move state handling atoms to global system var
This commit is contained in:
parent
7b3b509eaf
commit
73cf89f9d5
2 changed files with 21 additions and 20 deletions
|
@ -48,14 +48,14 @@
|
|||
:db {:name "grub-dev"}
|
||||
:port 3000})
|
||||
|
||||
(defn handle-websocket [handler]
|
||||
(defn handle-websocket [handler states]
|
||||
(fn [{:keys [websocket?] :as request}]
|
||||
(if websocket?
|
||||
(httpkit/with-channel request ws-channel
|
||||
(let [to-client (chan)
|
||||
from-client (chan)]
|
||||
(ws/add-connected-client! ws-channel to-client from-client)
|
||||
(state/sync-new-client! to-client from-client)))
|
||||
(state/sync-new-client! states to-client from-client)))
|
||||
(handler request))))
|
||||
|
||||
(defn handle-root [handler index]
|
||||
|
@ -64,18 +64,18 @@
|
|||
(resp/response index)
|
||||
(handler request))))
|
||||
|
||||
(defn make-handler [{:keys [index]}]
|
||||
(defn make-handler [{:keys [index]} states]
|
||||
(-> (fn [req] "Not found")
|
||||
(file/wrap-file "public")
|
||||
(content-type/wrap-content-type)
|
||||
(handle-root index)
|
||||
(handle-websocket)))
|
||||
(handle-websocket states)))
|
||||
|
||||
(defn start [current {:keys [port db] :as config}]
|
||||
(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) {:port port})]
|
||||
stop-server (httpkit/run-server (make-handler config states) {:port port})]
|
||||
(println "Started server on localhost:" port)
|
||||
(assoc config
|
||||
:db (merge (:db config) db)
|
||||
|
|
|
@ -7,7 +7,9 @@
|
|||
#+cljs (:require-macros [grub.macros :refer [log logs]]
|
||||
[cljs.core.async.macros :refer [go]]))
|
||||
|
||||
(defmulti handle-event (fn [event] (:type event)))
|
||||
(defmulti handle-event (fn [event]
|
||||
#+cljs (logs (:type event))
|
||||
(:type event)))
|
||||
|
||||
(defmethod handle-event :diff [{:keys [hash diff states shadow client?] :as msg}]
|
||||
(let [history-shadow (sync/get-history-state states hash)]
|
||||
|
@ -62,17 +64,14 @@
|
|||
([<remote >remote states] (make-agent true <remote >remote states))
|
||||
([<remote >remote states initial-shadow] (make-agent true <remote >remote states initial-shadow)))
|
||||
|
||||
(def states (atom []))
|
||||
(def empty-state sync/empty-state)
|
||||
|
||||
#+clj
|
||||
(defn sync-new-client! [>client <client]
|
||||
(defn sync-new-client! [states >client <client]
|
||||
(let [client-id (java.util.UUID/randomUUID)
|
||||
state-changes (chan)
|
||||
state-change-events (a/map< (fn [s] {:type :new-state :state s}) state-changes)
|
||||
state-change-events (chan 1 (map (fn [s] {:type :new-state :state s})))
|
||||
client-events (chan)]
|
||||
(add-watch states client-id (fn [_ _ _ new-states]
|
||||
(a/put! state-changes (sync/get-current-state new-states))))
|
||||
(add-watch states client-id
|
||||
(fn [_ _ _ new-states]
|
||||
(a/put! state-change-events (sync/get-current-state new-states))))
|
||||
(a/go-loop []
|
||||
(let [[val _] (a/alts! [<client state-change-events])]
|
||||
(if val
|
||||
|
@ -85,16 +84,18 @@
|
|||
|
||||
#+clj
|
||||
(defn init-server [to-db initial-state]
|
||||
(reset! states (sync/new-state initial-state))
|
||||
(add-watch states :to-db (fn [_ _ old-states new-states]
|
||||
(a/put! to-db (sync/get-current-state new-states)))))
|
||||
(let [states (atom (sync/new-state initial-state))]
|
||||
(add-watch states :to-db (fn [_ _ old-states new-states]
|
||||
(a/put! to-db (sync/get-current-state new-states))))
|
||||
states))
|
||||
|
||||
#+cljs
|
||||
(defn init-client [<remote >remote <view >view]
|
||||
(let [states (atom (sync/initial-state {} {}))]
|
||||
(let [states (atom (sync/initial-state {} {}))
|
||||
local-events (chan 1 (map (fn [s] {:type :new-state :state s})))]
|
||||
(add-watch states :render (fn [_ _ _ new-states]
|
||||
(let [new-state (sync/get-current-state new-states)]
|
||||
(a/put! >view new-state))))
|
||||
(a/pipe (a/map< (fn [s] {:type :new-state :state s}) <view) <remote)
|
||||
(make-client-agent <remote >remote states)
|
||||
(a/pipe <view local-events)
|
||||
(make-client-agent (a/merge [local-events <remote]) >remote states)
|
||||
(a/put! >remote message/full-sync-request)))
|
||||
|
|
Loading…
Add table
Reference in a new issue