From 4320401a4e989d84a9ba325d04d89738acd7e980 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Sat, 30 Aug 2014 16:43:58 +0300 Subject: [PATCH] Better - uses tx-listen --- src/clj/grub/state.clj | 16 ++----- src/cljs/grub/core.cljs | 9 ++-- src/cljs/grub/state.cljs | 80 ++++++++++++++++----------------- src/cljs/grub/view/app.cljs | 6 ++- src/cljx/grub/common_state.cljx | 12 +---- 5 files changed, 52 insertions(+), 71 deletions(-) diff --git a/src/clj/grub/state.clj b/src/clj/grub/state.clj index d8ee3d3..ed50836 100644 --- a/src/clj/grub/state.clj +++ b/src/clj/grub/state.clj @@ -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} (! 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) diff --git a/src/cljs/grub/state.cljs b/src/cljs/grub/state.cljs index a87db33..7e8410e 100644 --- a/src/cljs/grub/state.cljs +++ b/src/cljs/grub/state.cljs @@ -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] - (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)) - )))) - (go-loop [] - (if-let [{:keys [type diff hash shadow-hash] :as msg} ( complete sync") - (a/put! from cs/complete-sync-request))))) - :complete (do - (logs "Complete sync:" (hasch/uuid (:state msg))) - (reset! unacked-history {}) - (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)))) +(defn sync-state! [to from reset? state-changes] + (go-loop [] + (when-let [current-state ( 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") + (reset! unacked-states {}) + (reset! server-state (:state msg)) + (reset! state (:state msg))) + (logs "Invalid msg:" msg)) + (recur)) + (remove-watch state :state))) + (if reset? + (a/put! from cs/complete-sync-request) + (a/put! from (cs/diff-states @server-state @state)))) diff --git a/src/cljs/grub/view/app.cljs b/src/cljs/grub/view/app.cljs index 99db695..3b49fc5 100644 --- a/src/cljs/grub/view/app.cljs +++ b/src/cljs/grub/view/app.cljs @@ -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 :type) add-grubs-ch (chan)] @@ -34,4 +34,6 @@ {:target (.getElementById js/document "container") :shared {:>events >events :" hash) - )) + msg))