Patch diff onto state

This commit is contained in:
Nicholas Kariniemi 2014-08-09 16:03:06 +03:00
parent d775f1dab7
commit 5230dc9601
2 changed files with 19 additions and 3 deletions

View file

@ -9,13 +9,24 @@
(defn updated [a b]
(second (data/diff a b)))
(defn diff [a b]
(defn diff-maps [a b]
{:deleted (deleted a b)
:updated (changed a b)})
(defn diff-states [prev next]
(->> prev
(keys)
(map (fn [k] [k (diff (k prev) (k next))]))
(map (fn [k] [k (diff-maps (k prev) (k next))]))
(into {})))
(defn patch-map [state diff]
(-> state
(#(apply dissoc % (into [] (:deleted diff))))
(#(merge-with merge % (:updated diff)))))
(defn patch-state [state diff]
(->> state
(keys)
(map (fn [k] [k (patch-map (k state) (k diff))]))
(into {})))

View file

@ -73,4 +73,9 @@
{:completed false, :grub "Toothpaste", :id "grub-added"}}}})
(deftest diffing
(is (= (sync/diff-states server-state client-state) expected-diff)))
(is (= expected-diff (sync/diff-states server-state client-state))))
(deftest patching
(is
(let [diff (sync/diff-states server-state client-state)]
(= client-state (sync/patch-state server-state diff)))))