View takes in state changes, pushes out state changes
This commit is contained in:
parent
24a077b277
commit
423b962d2c
4 changed files with 27 additions and 28 deletions
|
@ -6,11 +6,12 @@
|
|||
(:require-macros [grub.macros :refer [log logs]]))
|
||||
|
||||
(defn init-app []
|
||||
(let [current-state (atom state/empty-state)
|
||||
state-changes (view/render-app current-state)
|
||||
(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 state-changes current-state)))
|
||||
(state/init-client from-remote to-remote local-states remote-states)))
|
||||
|
||||
(init-app)
|
||||
|
|
|
@ -25,11 +25,11 @@
|
|||
(dom/on-document-mousedown #(put! >events {:type :body-mousedown :event %}))
|
||||
(dom/on-window-scroll #(put! >events {:type :body-scroll :event %}))))))
|
||||
|
||||
(defn render-app [state]
|
||||
(let [>events (chan)
|
||||
(defn render-app [initial-state remote-states local-states]
|
||||
(let [state (atom initial-state)
|
||||
>events (chan)
|
||||
<events (a/pub >events :type)
|
||||
add-grubs-ch (chan)
|
||||
state-changes (chan)]
|
||||
add-grubs-ch (chan)]
|
||||
(om/root app-view
|
||||
state
|
||||
{:target (.getElementById js/document "container")
|
||||
|
@ -37,5 +37,7 @@
|
|||
:<events <events
|
||||
:add-grubs-ch add-grubs-ch}
|
||||
:tx-listen (fn [{:keys [new-state]} _]
|
||||
(put! state-changes new-state))})
|
||||
state-changes))
|
||||
(put! local-states new-state))})
|
||||
(go (loop [] (when-let [new-state (<! remote-states)]
|
||||
(reset! state new-state)
|
||||
(recur))))))
|
||||
|
|
|
@ -21,23 +21,19 @@
|
|||
(log "Connected:" event)
|
||||
(send-pending-msg websocket))
|
||||
|
||||
(defn on-message [from event]
|
||||
(let [msg (cljs.reader/read-string (.-message event))]
|
||||
(a/put! from msg)))
|
||||
(defn read-msg [msg]
|
||||
(cljs.reader/read-string (.-message msg)))
|
||||
|
||||
(def ws (atom nil))
|
||||
|
||||
(defn connect-client! [to from]
|
||||
(defn connect-client! [in out]
|
||||
(let [handler (goog.events.EventHandler.)
|
||||
websocket (goog.net.WebSocket.)
|
||||
listen (fn [type fun] (.listen handler websocket type fun false))]
|
||||
(reset! ws websocket)
|
||||
(listen goog.net.WebSocket.EventType.OPENED (partial on-connected websocket))
|
||||
(listen goog.net.WebSocket.EventType.MESSAGE (partial on-message from))
|
||||
(listen goog.net.WebSocket.EventType.MESSAGE #(a/put! out (read-msg %)))
|
||||
(listen goog.net.WebSocket.EventType.CLOSED #(log "Closed:" %))
|
||||
(listen goog.net.WebSocket.EventType.ERROR #(log "Error:" %))
|
||||
(go (loop []
|
||||
(when-let [msg (<! to)]
|
||||
(when-let [msg (<! in)]
|
||||
(reset! pending-msg msg)
|
||||
(send-pending-msg websocket)
|
||||
(recur))))
|
||||
|
|
|
@ -88,13 +88,13 @@
|
|||
(a/put! to-db (sync/get-current-state new-states)))))
|
||||
|
||||
#+cljs
|
||||
(defn init-client [in out state-changes current-state]
|
||||
(reset! states (sync/initial-state {} {}))
|
||||
(add-watch states :render (fn [_ _ _ new-states]
|
||||
(let [new-state (sync/get-current-state new-states)]
|
||||
(reset! current-state new-state))))
|
||||
(a/pipe (a/map< (fn [s]
|
||||
(swap! states sync/add-history-state s)
|
||||
{:type :new-state :state s}) state-changes) in)
|
||||
(make-client-agent in out states)
|
||||
(a/put! out message/full-sync-request))
|
||||
(defn init-client [<remote >remote <view >view]
|
||||
(let [states (atom (sync/initial-state {} {}))]
|
||||
(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]
|
||||
(swap! states sync/add-history-state s)
|
||||
{:type :new-state :state s}) <view) <remote)
|
||||
(make-client-agent <remote >remote states)
|
||||
(a/put! >remote message/full-sync-request)))
|
||||
|
|
Loading…
Reference in a new issue