Make server, client agent APIs more similar
This commit is contained in:
parent
b5a668e6ee
commit
7ada33bdfc
4 changed files with 11 additions and 15 deletions
|
@ -63,7 +63,7 @@
|
|||
(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)))
|
||||
(state/sync-new-client! from-client to-client state)))
|
||||
(handler request))))
|
||||
|
||||
(defn handle-root [handler index]
|
||||
|
|
|
@ -16,12 +16,11 @@
|
|||
|
||||
(defn start [system]
|
||||
(let [local-states (chan)
|
||||
remote-states (chan)
|
||||
to-remote (chan)
|
||||
from-remote (chan)
|
||||
view-state (view/render-app state/empty-state remote-states local-states)
|
||||
view-state (view/render-app state/empty-state local-states)
|
||||
ws (websocket/connect (:pending-msg system) to-remote from-remote)
|
||||
agent-states (state/sync-client! from-remote to-remote local-states remote-states)]
|
||||
agent-states (state/sync-client! from-remote to-remote local-states view-state)]
|
||||
(assoc system
|
||||
:ws ws
|
||||
:channels {:local-states local-states
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
(dom/on-document-mousedown #(put! >events {:type :body-mousedown :event %}))
|
||||
(dom/on-window-scroll #(put! >events {:type :body-scroll :event %}))))))
|
||||
|
||||
(defn render-app [initial-state <remote >remote]
|
||||
(defn render-app [initial-state >new-states]
|
||||
(let [state (atom initial-state)
|
||||
>events (chan)
|
||||
<events (a/pub >events :type)
|
||||
|
@ -37,8 +37,5 @@
|
|||
:<events <events
|
||||
:add-grubs-ch add-grubs-ch}
|
||||
:tx-listen (fn [{:keys [new-state tag]} _]
|
||||
(when (= tag :local) (put! >remote new-state)))})
|
||||
(go (loop [] (when-let [new-state (<! <remote)]
|
||||
(reset! state new-state)
|
||||
(recur))))
|
||||
(when (= tag :local) (put! >new-states new-state)))})
|
||||
state))
|
||||
|
|
|
@ -65,7 +65,7 @@
|
|||
(def make-client-agent (partial make-agent true))
|
||||
|
||||
#+clj
|
||||
(defn sync-new-client! [>client <client state]
|
||||
(defn sync-new-client! [<remote >remote 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})))
|
||||
|
@ -76,22 +76,22 @@
|
|||
(reset! state new-state)
|
||||
(a/put! state-change-events new-state)))))
|
||||
(a/go-loop []
|
||||
(let [[val _] (a/alts! [<client state-change-events])]
|
||||
(let [[val _] (a/alts! [<remote state-change-events])]
|
||||
(if val
|
||||
(do (>! client-events val)
|
||||
(recur))
|
||||
(do (remove-watch states client-id)
|
||||
(a/close! <client)
|
||||
(a/close! <remote)
|
||||
(a/close! state-change-events)))))
|
||||
(make-server-agent client-events >client states sync/empty-state)))
|
||||
(make-server-agent client-events >remote states sync/empty-state)))
|
||||
|
||||
#+cljs
|
||||
(defn sync-client! [<remote >remote <view >view]
|
||||
(defn sync-client! [<remote >remote <view 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))))
|
||||
(reset! state new-state))))
|
||||
(a/pipe <view local-events)
|
||||
(make-client-agent (a/merge [local-events <remote]) >remote states sync/empty-state)
|
||||
(a/put! >remote message/full-sync-request)
|
||||
|
|
Loading…
Add table
Reference in a new issue