Better - uses tx-listen
This commit is contained in:
parent
f087309c0f
commit
4320401a4e
5 changed files with 52 additions and 71 deletions
|
@ -14,17 +14,9 @@
|
|||
|
||||
(defn save-history-state [history new-state]
|
||||
(when-not (= (hasch/uuid (last history)) (hasch/uuid new-state))
|
||||
(println "Adding state to history: " (hasch/uuid new-state))
|
||||
(println "History:")
|
||||
(doseq [s (conj history new-state)]
|
||||
(println (hasch/uuid s)))
|
||||
(conj history new-state)))
|
||||
|
||||
(defn get-history-state [hash]
|
||||
(println "Look for history state:" hash)
|
||||
(println "History:")
|
||||
(doseq [s @state-history]
|
||||
(println (hasch/uuid s)))
|
||||
(first (filter #(= (hasch/uuid %) hash) @state-history)))
|
||||
|
||||
(add-watch state :history (fn [_ _ _ new-state]
|
||||
|
@ -40,18 +32,16 @@
|
|||
log (fn [& args]
|
||||
(apply println client-id args))]
|
||||
(add-watch state client-id (fn [_ _ _ current-state]
|
||||
(when-let [msg (cs/diff-states @client-state current-state)]
|
||||
(let [msg (cs/diff-states @client-state current-state)]
|
||||
(a/put! to msg)
|
||||
;; send ACK even if nothing changes
|
||||
;; TODO: reset only if send succeeds?
|
||||
(reset! client-state current-state))))
|
||||
(a/go-loop []
|
||||
(if-let [{:keys [type diff hash shadow-hash] :as msg} (<! from)]
|
||||
(do (condp = type
|
||||
:diff
|
||||
(if (= (hasch/uuid @client-state) shadow-hash)
|
||||
;; we have what they thought we had
|
||||
;; apply changes normally
|
||||
;; We have what they thought we had
|
||||
;; Apply changes normally
|
||||
(let [new-shadow (swap! client-state sync/patch-state diff)]
|
||||
(log "Hash matched state, apply changes")
|
||||
(if (= (hasch/uuid new-shadow) hash)
|
||||
|
|
|
@ -5,14 +5,15 @@
|
|||
[cljs.core.async :as a :refer [<! >! chan]])
|
||||
(:require-macros [grub.macros :refer [log logs]]))
|
||||
|
||||
(defn connect-to-server [reset?]
|
||||
(defn connect-to-server [reset? state-changes]
|
||||
(let [to-remote (chan)
|
||||
from-remote (chan)]
|
||||
(ws/connect-client! to-remote from-remote)
|
||||
(state/sync-state! from-remote to-remote reset?)))
|
||||
(state/sync-state! from-remote to-remote reset? state-changes)))
|
||||
|
||||
(defn init-app []
|
||||
(view/render-app state/state)
|
||||
(connect-to-server true))
|
||||
(let [state-changes (chan)]d
|
||||
(view/render-app state/state state-changes)
|
||||
(connect-to-server true state-changes)))
|
||||
|
||||
(init-app)
|
||||
|
|
|
@ -7,47 +7,45 @@
|
|||
[cljs.core.async.macros :refer [go go-loop]]))
|
||||
|
||||
(def state (atom cs/empty-state))
|
||||
(def server-state (atom cs/empty-state))
|
||||
|
||||
(def unacked-history (atom {}))
|
||||
(def unacked-states (atom {}))
|
||||
|
||||
(defn get-unacked-state [hash]
|
||||
(logs "Look for history state:" hash)
|
||||
(get @unacked-history hash))
|
||||
(defn get-server-state [hash]
|
||||
(if (= (hasch/uuid @server-state) hash)
|
||||
@server-state
|
||||
(get @unacked-states hash)))
|
||||
|
||||
(defn sync-state! [to from reset?]
|
||||
(let [server-state (atom cs/empty-state)]
|
||||
(add-watch state :state (fn [_ _ _ current-state]
|
||||
(defn sync-state! [to from reset? state-changes]
|
||||
(go-loop []
|
||||
(when-let [current-state (<! state-changes)]
|
||||
(when-not (= @server-state current-state)
|
||||
(let [msg (cs/diff-states @server-state current-state)]
|
||||
(when-not (get @unacked-history (hasch/uuid current-state))
|
||||
(logs "state change! msg: " msg)
|
||||
(swap! unacked-history assoc (hasch/uuid current-state) current-state)
|
||||
(logs "History:" (keys @unacked-history))
|
||||
(a/put! from msg))
|
||||
))))
|
||||
(swap! unacked-states assoc (:hash msg) current-state)
|
||||
(a/put! from msg)))
|
||||
(recur)))
|
||||
(go-loop []
|
||||
(if-let [{:keys [type diff hash shadow-hash] :as msg} (<! to)]
|
||||
(do (condp = type
|
||||
:diff (do
|
||||
(logs "Received diff:" msg)
|
||||
(when (not (= (hasch/uuid @server-state) shadow-hash))
|
||||
(reset! server-state (get-unacked-state shadow-hash)))
|
||||
(reset! unacked-history {})
|
||||
(let [ ;; what they now think we have (after updating)
|
||||
new-shadow (swap! server-state #(sync/patch-state % diff))]
|
||||
;; should match hash
|
||||
(if (= (hasch/uuid new-shadow) hash)
|
||||
;; apply same changes locally
|
||||
;; if there are differences, they will be sent back
|
||||
:diff
|
||||
(if-let [acked-server-state (get-server-state shadow-hash)]
|
||||
(do (reset! server-state acked-server-state)
|
||||
(reset! unacked-states {})
|
||||
(let [new-server (swap! server-state #(sync/patch-state % diff))]
|
||||
(if (= (hasch/uuid new-server) hash)
|
||||
(swap! state sync/patch-state diff)
|
||||
(do (log "Hash check failed --> complete sync")
|
||||
(do (log "State update failure --> complete sync")
|
||||
(a/put! from cs/complete-sync-request)))))
|
||||
(do (log "Could not find server state locally --> complete sync")
|
||||
(a/put! from cs/complete-sync-request)))
|
||||
:complete (do
|
||||
(logs "Complete sync:" (hasch/uuid (:state msg)))
|
||||
(reset! unacked-history {})
|
||||
(logs "Complete sync")
|
||||
(reset! unacked-states {})
|
||||
(reset! server-state (:state msg))
|
||||
(reset! state (:state msg)))
|
||||
(logs "Invalid msg:" msg))
|
||||
(recur))
|
||||
(remove-watch state :state)))
|
||||
(when reset? (a/put! from cs/complete-sync-request))))
|
||||
(if reset?
|
||||
(a/put! from cs/complete-sync-request)
|
||||
(a/put! from (cs/diff-states @server-state @state))))
|
||||
|
|
|
@ -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 [state]
|
||||
(defn render-app [state state-changes]
|
||||
(let [>events (chan)
|
||||
<events (a/pub >events :type)
|
||||
add-grubs-ch (chan)]
|
||||
|
@ -34,4 +34,6 @@
|
|||
{:target (.getElementById js/document "container")
|
||||
:shared {:>events >events
|
||||
:<events <events
|
||||
:add-grubs-ch add-grubs-ch}})))
|
||||
:add-grubs-ch add-grubs-ch}
|
||||
:tx-listen (fn [{:keys [new-state]} _]
|
||||
(put! state-changes new-state))})))
|
||||
|
|
|
@ -17,17 +17,7 @@
|
|||
|
||||
(defn diff-states [shadow state]
|
||||
(let [diff (sync/diff-states shadow state)
|
||||
;; what we now have
|
||||
hash (hasch/uuid state)
|
||||
|
||||
;; what we had/what you used to have
|
||||
;; should match what they think we have
|
||||
shadow-hash (hasch/uuid shadow)
|
||||
msg (diff-msg diff hash shadow-hash)]
|
||||
msg
|
||||
;(logs "Sync because:")
|
||||
;(logs "Local = " state)
|
||||
;(logs "Remote = " shadow)
|
||||
;(logs "Diff:" diff)
|
||||
;(logs "Send" shadow-hash "->" hash)
|
||||
))
|
||||
msg))
|
||||
|
|
Loading…
Reference in a new issue