Move state handling to shared code

This commit is contained in:
Nicholas Kariniemi 2014-09-25 23:16:57 +03:00
parent 39307f5a73
commit e6b3c10396
2 changed files with 9 additions and 10 deletions

View file

@ -7,8 +7,8 @@
;; Server state
(def states (atom []))
(defn make-server-agent
([in out states] (make-server-agent in out states sync/empty-state))
(defn make-agent
([in out states] (make-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)]
@ -35,8 +35,7 @@
:new-state
(let [{:keys [diff hash]} (sync/diff-states (:new-states msg) client-state)]
(>! out (message/diff-msg diff hash)))
(do (println "Unknown message:" msg)
(recur client-state)))))))
(recur client-state))))))
;; TODO: Remove watch, close up channels properly
(defn sync-new-client! [>client <client]
@ -46,7 +45,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-server-agent client-events >client states)))
(make-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-server-agent in out states*)
(state/make-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-server-agent in out states*)
(state/make-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-server-agent in out states*)
(state/make-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-server-agent in out states*)
(state/make-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-server-agent in out states* client-state)
(state/make-agent in out states* client-state)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states