Try breaking apart sync algorithm

This commit is contained in:
Nicholas Kariniemi 2014-09-07 09:29:06 +03:00
parent 49aa9c784d
commit 560b895a9b
2 changed files with 11 additions and 7 deletions

View file

@ -12,7 +12,7 @@
(state/sync-state! from-remote to-remote reset? state-changes))) (state/sync-state! from-remote to-remote reset? state-changes)))
(defn init-app [] (defn init-app []
(let [state-changes (view/render-app state/state state-changes)] (let [state-changes (view/render-app state/state)]
(connect-to-server true state-changes))) (connect-to-server true state-changes)))
(init-app) (init-app)

View file

@ -16,14 +16,16 @@
@server-state @server-state
(get @unacked-states hash))) (get @unacked-states hash)))
(defn sync-state! [to from reset? state-changes] (defn send-state-changes-to-server! [state-changes from]
(go-loop [] (go-loop []
(when-let [current-state (<! state-changes)] (when-let [current-state (<! state-changes)]
(when-not (= @server-state current-state) (when-not (= @server-state current-state)
(let [msg (cs/diff-states @server-state current-state)] (let [msg (cs/diff-states @server-state current-state)]
(swap! unacked-states assoc (:hash msg) current-state) (swap! unacked-states assoc (:hash msg) current-state)
(a/put! from msg))) (a/put! from msg)))
(recur))) (recur))))
(defn handle-received-changes! [to from]
(go-loop [] (go-loop []
(if-let [{:keys [type diff hash shadow-hash] :as msg} (<! to)] (if-let [{:keys [type diff hash shadow-hash] :as msg} (<! to)]
(do (condp = type (do (condp = type
@ -45,7 +47,9 @@
(reset! state (:state msg))) (reset! state (:state msg)))
(logs "Invalid msg:" msg)) (logs "Invalid msg:" msg))
(recur)) (recur))
(remove-watch state :state))) (remove-watch state :state))))
(if reset?
(a/put! from cs/complete-sync-request) (defn sync-state! [to from reset? state-changes]
(a/put! from (cs/diff-states @server-state @state)))) (send-state-changes-to-server! state-changes from)
(handle-received-changes! to from)
(a/put! from (if reset? cs/complete-sync-request (cs/diff-states @server-state @state))))