Push state history into state handling
This commit is contained in:
parent
dffdc7cabb
commit
b5a668e6ee
3 changed files with 27 additions and 27 deletions
|
@ -3,7 +3,6 @@
|
|||
[grub.db :as db]
|
||||
[grub.test.integration.core :as integration-test]
|
||||
[grub.state :as state]
|
||||
[grub.sync :as sync]
|
||||
[ring.middleware.file :as file]
|
||||
[ring.middleware.content-type :as content-type]
|
||||
[ring.util.response :as resp]
|
||||
|
@ -46,7 +45,7 @@
|
|||
:db-conn nil
|
||||
:port 3000
|
||||
:stop-server nil
|
||||
:states (atom nil)})
|
||||
:state (atom nil)})
|
||||
|
||||
(def dev-system
|
||||
{:index dev-index-page
|
||||
|
@ -55,16 +54,16 @@
|
|||
:db-conn nil
|
||||
:port 3000
|
||||
:stop-server nil
|
||||
:states (atom nil)})
|
||||
:state (atom nil)})
|
||||
|
||||
(defn handle-websocket [handler states]
|
||||
(defn handle-websocket [handler state]
|
||||
(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 states)))
|
||||
(state/sync-new-client! to-client from-client state)))
|
||||
(handler request))))
|
||||
|
||||
(defn handle-root [handler index]
|
||||
|
@ -73,33 +72,31 @@
|
|||
(resp/response index)
|
||||
(handler request))))
|
||||
|
||||
(defn make-handler [{:keys [index]} states]
|
||||
(defn make-handler [{:keys [index state]}]
|
||||
(-> (fn [req] "Not found")
|
||||
(file/wrap-file "public")
|
||||
(content-type/wrap-content-type)
|
||||
(handle-root index)
|
||||
(handle-websocket states)))
|
||||
(handle-websocket state)))
|
||||
|
||||
(defn start [current {:keys [port db-name states] :as system}]
|
||||
(let [to-db (chan)
|
||||
{:keys [db conn]} (db/connect db-name)
|
||||
_ (reset! states (sync/new-state (db/get-current-state db)))
|
||||
stop-server (httpkit/run-server (make-handler system states) {:port port})]
|
||||
(add-watch states :db (fn [_ _ old-states new-states]
|
||||
(db/update-db! db (sync/get-current-state new-states))))
|
||||
(defn start [current {:keys [port db-name state] :as system}]
|
||||
(let [{:keys [db conn]} (db/connect db-name)
|
||||
_ (reset! state (db/get-current-state db))
|
||||
stop-server (httpkit/run-server (make-handler system) {:port port})]
|
||||
(add-watch state :db (fn [_ _ old new]
|
||||
(db/update-db! db new)))
|
||||
(println "Started server on localhost:" port)
|
||||
(assoc system
|
||||
:db db
|
||||
:db-conn conn
|
||||
:stop-server stop-server
|
||||
:states states)))
|
||||
:state state)))
|
||||
|
||||
|
||||
(defn stop [{:keys [db stop-server states] :as system}]
|
||||
(remove-watch states :db)
|
||||
(defn stop [{:keys [db-conn stop-server state] :as system}]
|
||||
(remove-watch state :db)
|
||||
(stop-server)
|
||||
(db/disconnect (:conn db))
|
||||
(reset! states nil)
|
||||
(db/disconnect db-conn)
|
||||
(reset! state nil)
|
||||
system)
|
||||
|
||||
(defn usage [options-summary]
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
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)]
|
||||
agent-states (state/sync-client! from-remote to-remote local-states remote-states)]
|
||||
(assoc system
|
||||
:ws ws
|
||||
:channels {:local-states local-states
|
||||
|
|
|
@ -65,13 +65,16 @@
|
|||
(def make-client-agent (partial make-agent true))
|
||||
|
||||
#+clj
|
||||
(defn sync-new-client! [>client <client states]
|
||||
(let [client-id (java.util.UUID/randomUUID)
|
||||
(defn sync-new-client! [>client <client state]
|
||||
(let [states (atom (sync/new-state @state))
|
||||
client-id (java.util.UUID/randomUUID)
|
||||
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-change-events (sync/get-current-state new-states))))
|
||||
(add-watch states client-id (fn [_ _ _ new-states]
|
||||
(let [new-state (sync/get-current-state new-states)]
|
||||
(when new-state
|
||||
(reset! state new-state)
|
||||
(a/put! state-change-events new-state)))))
|
||||
(a/go-loop []
|
||||
(let [[val _] (a/alts! [<client state-change-events])]
|
||||
(if val
|
||||
|
@ -83,7 +86,7 @@
|
|||
(make-server-agent client-events >client states sync/empty-state)))
|
||||
|
||||
#+cljs
|
||||
(defn init-client [<remote >remote <view >view]
|
||||
(defn sync-client! [<remote >remote <view >view]
|
||||
(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]
|
||||
|
|
Loading…
Reference in a new issue