Rename sync -> diff

This commit is contained in:
Nicholas Kariniemi 2014-09-14 21:30:19 +03:00
parent ad335d0b23
commit 5a66361746
6 changed files with 26 additions and 28 deletions

View file

@ -1,5 +1,5 @@
(ns grub.state (ns grub.state
(:require [grub.sync :as sync] (:require [grub.diff :as diff]
[grub.util :as util] [grub.util :as util]
[grub.common-state :as cs] [grub.common-state :as cs]
[clojure.core.async :as a :refer [<! >! chan go]] [clojure.core.async :as a :refer [<! >! chan go]]
@ -42,10 +42,10 @@
(if (= (hasch/uuid @client-state) shadow-hash) (if (= (hasch/uuid @client-state) shadow-hash)
;; We have what they thought we had ;; We have what they thought we had
;; Apply changes normally ;; 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") (log "Hash matched state, apply changes")
(if (= (hasch/uuid new-shadow) hash) (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)) (>! @to-db diff))
(do (log "Applying diff failed --> full sync") (do (log "Applying diff failed --> full sync")
(let [sync-state @state] (let [sync-state @state]
@ -60,9 +60,9 @@
;; reset client state to this and continue as normal ;; reset client state to this and continue as normal
(do (do
(reset! client-state history-state) (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) (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)) (>! @to-db diff))
(do (log "Applying diff failed --> full sync") (do (log "Applying diff failed --> full sync")
(let [sync-state @state] (let [sync-state @state]

View file

@ -1,5 +1,5 @@
(ns grub.state (ns grub.state
(:require [grub.sync :as sync] (:require [grub.diff :as diff]
[grub.common-state :as cs] [grub.common-state :as cs]
[cljs.core.async :as a :refer [<! >! chan]] [cljs.core.async :as a :refer [<! >! chan]]
[hasch.core :as hasch]) [hasch.core :as hasch])
@ -33,9 +33,9 @@
(if-let [acked-server-state (get-server-state shadow-hash)] (if-let [acked-server-state (get-server-state shadow-hash)]
(do (reset! server-state acked-server-state) (do (reset! server-state acked-server-state)
(reset! unacked-states {}) (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) (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") (do (log "State update failure --> complete sync")
(a/put! from cs/complete-sync-request))))) (a/put! from cs/complete-sync-request)))))
(do (log "Could not find server state locally --> complete sync") (do (log "Could not find server state locally --> complete sync")

View file

@ -1,7 +1,5 @@
(ns grub.websocket (ns grub.websocket
(:require [grub.state :as state] (:require [cljs.core.async :as a :refer [<! >! chan]]
[grub.sync :as sync]
[cljs.core.async :as a :refer [<! >! chan]]
[cljs.reader] [cljs.reader]
goog.net.WebSocket goog.net.WebSocket
goog.events.EventHandler goog.events.EventHandler

View file

@ -1,5 +1,5 @@
(ns grub.common-state (ns grub.common-state
(:require [grub.sync :as sync] (:require [grub.diff :as diff]
[hasch.core :as hasch])) [hasch.core :as hasch]))
(def empty-state {:grubs {} :recipes {}}) (def empty-state {:grubs {} :recipes {}})
@ -16,7 +16,7 @@
:shadow-hash shadow-hash}) :shadow-hash shadow-hash})
(defn diff-states [shadow state] (defn diff-states [shadow state]
(let [diff (sync/diff-states shadow state) (let [diff (diff/diff-states shadow state)
hash (hasch/uuid state) hash (hasch/uuid state)
shadow-hash (hasch/uuid shadow) shadow-hash (hasch/uuid shadow)
msg (diff-msg diff hash shadow-hash)] msg (diff-msg diff hash shadow-hash)]

View file

@ -1,4 +1,4 @@
(ns grub.sync (ns grub.diff
(:require [clojure.data :as data] (:require [clojure.data :as data]
[clojure.set :as set])) [clojure.set :as set]))

View file

@ -1,5 +1,5 @@
(ns grub.test.unit.sync (ns grub.test.unit.diff
(:require [grub.sync :as sync] (:require [grub.diff :as diff]
[clojure.test :refer :all])) [clojure.test :refer :all]))
@ -9,39 +9,39 @@
(deftest diff-empty-states (deftest diff-empty-states
(let [empty-state {:grubs {} :recipes {}}] (let [empty-state {:grubs {} :recipes {}}]
(is (= empty-diff (is (= empty-diff
(sync/diff-states empty-state empty-state))))) (diff/diff-states empty-state empty-state)))))
(deftest diff-equal-states (deftest diff-equal-states
(is (= empty-diff (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 {}})))) {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}))))
(deftest diff-added-grub (deftest diff-added-grub
(is (= {:grubs {:deleted #{} (is (= {:grubs {:deleted #{}
:updated {"id" {:completed false, :text "asdf"}}} :updated {"id" {:completed false, :text "asdf"}}}
:recipes {:deleted #{} :updated nil}} :recipes {:deleted #{} :updated nil}}
(sync/diff-states {:grubs {} :recipes {}} (diff/diff-states {:grubs {} :recipes {}}
{:grubs {"id" {:text "asdf" :completed false}} :recipes {}})))) {:grubs {"id" {:text "asdf" :completed false}} :recipes {}}))))
(deftest diff-deleted-grub (deftest diff-deleted-grub
(is (= {:grubs {:deleted #{"id"} (is (= {:grubs {:deleted #{"id"}
:updated nil} :updated nil}
:recipes {:deleted #{} :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 {}})))) {:grubs {} :recipes {}}))))
(deftest diff-edited-grub (deftest diff-edited-grub
(is (= {:grubs {:deleted #{} (is (= {:grubs {:deleted #{}
:updated {"id" {:text "asdf2"}}} :updated {"id" {:text "asdf2"}}}
:recipes {:deleted #{} :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 {"id" {:text "asdf2" :completed false}} :recipes {}})))) {:grubs {"id" {:text "asdf2" :completed false}} :recipes {}}))))
(deftest diff-completed-grub (deftest diff-completed-grub
(is (= {:grubs {:deleted #{} (is (= {:grubs {:deleted #{}
:updated {"id" {:completed true}}} :updated {"id" {:completed true}}}
:recipes {:deleted #{} :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 {"id" {:text "asdf" :completed true}} :recipes {}})))) {:grubs {"id" {:text "asdf" :completed true}} :recipes {}}))))
(deftest diff-added-recipe (deftest diff-added-recipe
@ -49,7 +49,7 @@
:updated nil} :updated nil}
:recipes {:deleted #{} :updated {"id" {:name "Blue Cheese Soup" :recipes {:deleted #{} :updated {"id" {:name "Blue Cheese Soup"
:grubs "Some grubs"}}}} :grubs "Some grubs"}}}}
(sync/diff-states {:grubs {} :recipes {}} (diff/diff-states {:grubs {} :recipes {}}
{:grubs {} :recipes {"id" {:name "Blue Cheese Soup" {:grubs {} :recipes {"id" {:name "Blue Cheese Soup"
:grubs "Some grubs"}}})))) :grubs "Some grubs"}}}))))
@ -57,7 +57,7 @@
(is (= {:grubs {:deleted #{} (is (= {:grubs {:deleted #{}
:updated nil} :updated nil}
:recipes {:deleted #{} :updated {"id" {:name "Bleu Cheese Soup" }}}} :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 "Some grubs"}}}
{:grubs {} :recipes {"id" {:name "Bleu Cheese Soup" {:grubs {} :recipes {"id" {:name "Bleu Cheese Soup"
:grubs "Some grubs"}}})))) :grubs "Some grubs"}}}))))
@ -65,7 +65,7 @@
(deftest diff-deleted-recipe (deftest diff-deleted-recipe
(is (= {:grubs {:deleted #{} :updated nil} (is (= {:grubs {:deleted #{} :updated nil}
:recipes {:deleted #{"id"} :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 "Some grubs"}}}
{:grubs {} :recipes {}})))) {:grubs {} :recipes {}}))))
@ -125,9 +125,9 @@
{:completed false :text "Toothpaste"}}}}) {:completed false :text "Toothpaste"}}}})
(deftest diff-many-changes (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 (deftest patch-returns-original-state
(is (is
(let [diff (sync/diff-states before-state after-state)] (let [diff (diff/diff-states before-state after-state)]
(= after-state (sync/patch-state before-state diff))))) (= after-state (diff/patch-state before-state diff)))))