Merge client and server state handling -- wip

This commit is contained in:
Nicholas Kariniemi 2014-09-25 23:23:29 +03:00
parent e6b3c10396
commit d01814fe3c
2 changed files with 37 additions and 8 deletions

View file

@ -7,8 +7,8 @@
;; Server state
(def states (atom []))
(defn make-agent
([in out states] (make-agent in out states sync/empty-state))
(defn make-server-agent
([in out states] (make-server-agent in out states sync/empty-state))
([in out states initial-client-state]
(a/go-loop [client-state initial-client-state]
(when-let [msg (<! in)]
@ -37,6 +37,35 @@
(>! out (message/diff-msg diff hash)))
(recur client-state))))))
(defn make-client-agent
([in out states] (make-client-agent in out states sync/empty-state))
([in out states initial-server-state]
(a/go-loop [server-state initial-server-state]
(when-let [msg (<! in)]
(condp = (:type msg)
:diff
(let [states* @states
shadow (sync/get-history-state states* (:hash msg))]
(if shadow
(let [new-states (sync/apply-diff states* (:diff msg))
new-shadow (diff/patch-state shadow (:diff msg))
{:keys [diff hash]} (sync/diff-states new-states new-shadow)]
(reset! states new-states)
(recur new-shadow))
(let [state (sync/get-current-state @states)]
(>! out (message/full-sync state))
(recur state))))
:full-sync
(let [state (:state msg)]
(reset! states [state])
(recur state))
:new-state
(let [{:keys [diff hash]} (sync/diff-states (:new-states msg) server-state)]
(>! out (message/diff-msg diff hash)))
(recur server-state))))))
;; TODO: Remove watch, close up channels properly
(defn sync-new-client! [>client <client]
(let [client-id (java.util.UUID/randomUUID)
@ -45,7 +74,7 @@
client-events (chan)]
(add-watch states client-id (fn [_ _ _ new-states] (a/put! state-changes new-states)))
(a/pipe (a/merge [<client state-change-events]) client-events)
(make-agent client-events >client states)))
(make-server-agent client-events >client states)))
(defn init [to-db grubs recipes]
(reset! states (sync/initial-state grubs recipes))

View file

@ -21,7 +21,7 @@
:hash (:hash (first states))}
in (chan 1)
out (chan 1)]
(state/make-agent in out states*)
(state/make-server-agent in out states*)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states
@ -48,7 +48,7 @@
:hash (:hash (first states))}
in (chan 1)
out (chan 1)]
(state/make-agent in out states*)
(state/make-server-agent in out states*)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states
@ -83,7 +83,7 @@
:recipes {}})}
in (chan 1)
out (chan 1)]
(state/make-agent in out states*)
(state/make-server-agent in out states*)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states
@ -109,7 +109,7 @@
msg {:type :full-sync}
in (chan 1)
out (chan 1)]
(state/make-agent in out states*)
(state/make-server-agent in out states*)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states
@ -141,7 +141,7 @@
:new-states states}
in (chan 1)
out (chan 1)]
(state/make-agent in out states* client-state)
(state/make-server-agent in out states* client-state)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states