From 12ac165f241089cc25f97f49befe7a4467e64ac5 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Mon, 6 Oct 2014 22:13:50 +0300 Subject: [PATCH] Store max 20 history states, reset on client when server ACKs --- src/cljx/grub/state.cljx | 6 ++++-- src/cljx/grub/sync.cljx | 7 ++++++- src/test/grub/test/unit/state.clj | 2 -- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/cljx/grub/state.cljx b/src/cljx/grub/state.cljx index 265abe3..5edd4ac 100644 --- a/src/cljx/grub/state.cljx +++ b/src/cljx/grub/state.cljx @@ -20,8 +20,10 @@ (let [new-states (sync/apply-diff states* (:diff msg)) new-shadow (diff/patch-state shadow (:diff msg)) {:keys [diff hash]} (sync/diff-states (sync/get-current-state new-states) new-shadow)] - (when-not (= states* new-states) - (reset! states new-states)) + (if client? + (reset! states (sync/new-state (sync/get-current-state new-states))) + (when-not (= states* new-states) + (reset! states new-states))) (when-not (or client? (sync/empty-diff? (:diff msg))) (>! out (message/diff-msg diff hash))) (if client? diff --git a/src/cljx/grub/sync.cljx b/src/cljx/grub/sync.cljx index 033449b..3da74e1 100644 --- a/src/cljx/grub/sync.cljx +++ b/src/cljx/grub/sync.cljx @@ -3,6 +3,8 @@ [grub.util :as util] [hasch.core :as hasch])) +(def num-history-states 20) + (def empty-state {:grubs {} :recipes {}}) (defn initial-state [grubs recipes] @@ -25,7 +27,10 @@ new-hash (hasch/uuid new-state)] (if (= last-hash new-hash) states - (conj states {:hash new-hash :state new-state})))) + (let [new-states (conj states {:hash new-hash :state new-state})] + (if (>= (count states) num-history-states) + (into [] (rest new-states)) + new-states))))) (defn diff-states [states shadow] (let [state states;(get-current-state states) diff --git a/src/test/grub/test/unit/state.clj b/src/test/grub/test/unit/state.clj index e241c1b..ce91379 100644 --- a/src/test/grub/test/unit/state.clj +++ b/src/test/grub/test/unit/state.clj @@ -197,8 +197,6 @@ (>!! client-in msg) ( (hashed-states - {:grubs {"1" {:completed false, :text "2 apples"}}, :recipes {}} - {:grubs {"1" {:completed true, :text "2 apples"}}, :recipes {}} {:grubs {"1" {:completed true, :text "4 apples"}}, :recipes {}}) @server-states => (hashed-states {:grubs {"1" {:completed false, :text "2 apples"}}, :recipes {}}