Store max 20 history states, reset on client when server ACKs
This commit is contained in:
parent
f0e16817b5
commit
12ac165f24
3 changed files with 10 additions and 5 deletions
|
@ -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?
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -197,8 +197,6 @@
|
|||
(>!! client-in msg)
|
||||
(<!!? client-state-changes)
|
||||
@client-states => (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 {}}
|
||||
|
|
Loading…
Reference in a new issue