Rename sync -> diff
This commit is contained in:
parent
ad335d0b23
commit
5a66361746
6 changed files with 26 additions and 28 deletions
|
@ -1,5 +1,5 @@
|
|||
(ns grub.state
|
||||
(:require [grub.sync :as sync]
|
||||
(:require [grub.diff :as diff]
|
||||
[grub.util :as util]
|
||||
[grub.common-state :as cs]
|
||||
[clojure.core.async :as a :refer [<! >! chan go]]
|
||||
|
@ -42,10 +42,10 @@
|
|||
(if (= (hasch/uuid @client-state) shadow-hash)
|
||||
;; We have what they thought we had
|
||||
;; Apply changes normally
|
||||
(let [new-shadow (swap! client-state sync/patch-state diff)]
|
||||
(let [new-shadow (swap! client-state diff/patch-state diff)]
|
||||
(log "Hash matched state, apply changes")
|
||||
(if (= (hasch/uuid new-shadow) hash)
|
||||
(let [new-state (swap! state sync/patch-state diff)]
|
||||
(let [new-state (swap! state diff/patch-state diff)]
|
||||
(>! @to-db diff))
|
||||
(do (log "Applying diff failed --> full sync")
|
||||
(let [sync-state @state]
|
||||
|
@ -60,9 +60,9 @@
|
|||
;; reset client state to this and continue as normal
|
||||
(do
|
||||
(reset! client-state history-state)
|
||||
(let [new-shadow (swap! client-state sync/patch-state diff)]
|
||||
(let [new-shadow (swap! client-state diff/patch-state diff)]
|
||||
(if (= (hasch/uuid new-shadow) hash)
|
||||
(let [new-state (swap! state sync/patch-state diff)]
|
||||
(let [new-state (swap! state diff/patch-state diff)]
|
||||
(>! @to-db diff))
|
||||
(do (log "Applying diff failed --> full sync")
|
||||
(let [sync-state @state]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
(ns grub.state
|
||||
(:require [grub.sync :as sync]
|
||||
(:require [grub.diff :as diff]
|
||||
[grub.common-state :as cs]
|
||||
[cljs.core.async :as a :refer [<! >! chan]]
|
||||
[hasch.core :as hasch])
|
||||
|
@ -33,9 +33,9 @@
|
|||
(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))]
|
||||
(let [new-server (swap! server-state #(diff/patch-state % diff))]
|
||||
(if (= (hasch/uuid new-server) hash)
|
||||
(swap! state sync/patch-state diff)
|
||||
(swap! state diff/patch-state diff)
|
||||
(do (log "State update failure --> complete sync")
|
||||
(a/put! from cs/complete-sync-request)))))
|
||||
(do (log "Could not find server state locally --> complete sync")
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
(ns grub.websocket
|
||||
(:require [grub.state :as state]
|
||||
[grub.sync :as sync]
|
||||
[cljs.core.async :as a :refer [<! >! chan]]
|
||||
(:require [cljs.core.async :as a :refer [<! >! chan]]
|
||||
[cljs.reader]
|
||||
goog.net.WebSocket
|
||||
goog.events.EventHandler
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
(ns grub.common-state
|
||||
(:require [grub.sync :as sync]
|
||||
(:require [grub.diff :as diff]
|
||||
[hasch.core :as hasch]))
|
||||
|
||||
(def empty-state {:grubs {} :recipes {}})
|
||||
|
@ -16,7 +16,7 @@
|
|||
:shadow-hash shadow-hash})
|
||||
|
||||
(defn diff-states [shadow state]
|
||||
(let [diff (sync/diff-states shadow state)
|
||||
(let [diff (diff/diff-states shadow state)
|
||||
hash (hasch/uuid state)
|
||||
shadow-hash (hasch/uuid shadow)
|
||||
msg (diff-msg diff hash shadow-hash)]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
(ns grub.sync
|
||||
(ns grub.diff
|
||||
(:require [clojure.data :as data]
|
||||
[clojure.set :as set]))
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
(ns grub.test.unit.sync
|
||||
(:require [grub.sync :as sync]
|
||||
(ns grub.test.unit.diff
|
||||
(:require [grub.diff :as diff]
|
||||
[clojure.test :refer :all]))
|
||||
|
||||
|
||||
|
@ -9,39 +9,39 @@
|
|||
(deftest diff-empty-states
|
||||
(let [empty-state {:grubs {} :recipes {}}]
|
||||
(is (= empty-diff
|
||||
(sync/diff-states empty-state empty-state)))))
|
||||
(diff/diff-states empty-state empty-state)))))
|
||||
|
||||
(deftest diff-equal-states
|
||||
(is (= empty-diff
|
||||
(sync/diff-states {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}
|
||||
(diff/diff-states {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}
|
||||
{:grubs {"id" {:text "asdf" :completed false}} :recipes {}}))))
|
||||
|
||||
(deftest diff-added-grub
|
||||
(is (= {:grubs {:deleted #{}
|
||||
:updated {"id" {:completed false, :text "asdf"}}}
|
||||
:recipes {:deleted #{} :updated nil}}
|
||||
(sync/diff-states {:grubs {} :recipes {}}
|
||||
(diff/diff-states {:grubs {} :recipes {}}
|
||||
{:grubs {"id" {:text "asdf" :completed false}} :recipes {}}))))
|
||||
|
||||
(deftest diff-deleted-grub
|
||||
(is (= {:grubs {:deleted #{"id"}
|
||||
:updated nil}
|
||||
:recipes {:deleted #{} :updated nil}}
|
||||
(sync/diff-states {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}
|
||||
(diff/diff-states {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}
|
||||
{:grubs {} :recipes {}}))))
|
||||
|
||||
(deftest diff-edited-grub
|
||||
(is (= {:grubs {:deleted #{}
|
||||
:updated {"id" {:text "asdf2"}}}
|
||||
:recipes {:deleted #{} :updated nil}}
|
||||
(sync/diff-states {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}
|
||||
(diff/diff-states {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}
|
||||
{:grubs {"id" {:text "asdf2" :completed false}} :recipes {}}))))
|
||||
|
||||
(deftest diff-completed-grub
|
||||
(is (= {:grubs {:deleted #{}
|
||||
:updated {"id" {:completed true}}}
|
||||
:recipes {:deleted #{} :updated nil}}
|
||||
(sync/diff-states {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}
|
||||
(diff/diff-states {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}
|
||||
{:grubs {"id" {:text "asdf" :completed true}} :recipes {}}))))
|
||||
|
||||
(deftest diff-added-recipe
|
||||
|
@ -49,7 +49,7 @@
|
|||
:updated nil}
|
||||
:recipes {:deleted #{} :updated {"id" {:name "Blue Cheese Soup"
|
||||
:grubs "Some grubs"}}}}
|
||||
(sync/diff-states {:grubs {} :recipes {}}
|
||||
(diff/diff-states {:grubs {} :recipes {}}
|
||||
{:grubs {} :recipes {"id" {:name "Blue Cheese Soup"
|
||||
:grubs "Some grubs"}}}))))
|
||||
|
||||
|
@ -57,7 +57,7 @@
|
|||
(is (= {:grubs {:deleted #{}
|
||||
:updated nil}
|
||||
:recipes {:deleted #{} :updated {"id" {:name "Bleu Cheese Soup" }}}}
|
||||
(sync/diff-states {:grubs {} :recipes {"id" {:name "Blue Cheese Soup"
|
||||
(diff/diff-states {:grubs {} :recipes {"id" {:name "Blue Cheese Soup"
|
||||
:grubs "Some grubs"}}}
|
||||
{:grubs {} :recipes {"id" {:name "Bleu Cheese Soup"
|
||||
:grubs "Some grubs"}}}))))
|
||||
|
@ -65,7 +65,7 @@
|
|||
(deftest diff-deleted-recipe
|
||||
(is (= {:grubs {:deleted #{} :updated nil}
|
||||
:recipes {:deleted #{"id"} :updated nil}}
|
||||
(sync/diff-states {:grubs {} :recipes {"id" {:name "Blue Cheese Soup"
|
||||
(diff/diff-states {:grubs {} :recipes {"id" {:name "Blue Cheese Soup"
|
||||
:grubs "Some grubs"}}}
|
||||
{:grubs {} :recipes {}}))))
|
||||
|
||||
|
@ -125,9 +125,9 @@
|
|||
{:completed false :text "Toothpaste"}}}})
|
||||
|
||||
(deftest diff-many-changes
|
||||
(is (= expected-diff (sync/diff-states before-state after-state))))
|
||||
(is (= expected-diff (diff/diff-states before-state after-state))))
|
||||
|
||||
(deftest patch-returns-original-state
|
||||
(is
|
||||
(let [diff (sync/diff-states before-state after-state)]
|
||||
(= after-state (sync/patch-state before-state diff)))))
|
||||
(let [diff (diff/diff-states before-state after-state)]
|
||||
(= after-state (diff/patch-state before-state diff)))))
|
Loading…
Reference in a new issue