From bef0e8d4e773ff88088224cd67b730b97528c95d Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Fri, 10 Oct 2014 19:03:33 +0300 Subject: [PATCH] Separate event handling into handler methods --- src/cljx/grub/state.cljx | 82 ++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/src/cljx/grub/state.cljx b/src/cljx/grub/state.cljx index 06539d9..7f8e12c 100644 --- a/src/cljx/grub/state.cljx +++ b/src/cljx/grub/state.cljx @@ -7,51 +7,53 @@ #+cljs (:require-macros [grub.macros :refer [log logs]] [cljs.core.async.macros :refer [go]])) +(defmulti handle-message (fn [msg out states shadow client?] (:type msg))) + +(defmethod handle-message :diff [msg out states shadow client?] + (let [states* @states + shadow (sync/get-history-state states* (:hash msg))] + (if shadow + (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)] + (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))) + (a/put! out (message/diff-msg diff hash))) + (if client? + new-shadow + (sync/get-current-state new-states))) + (if client? + (do (a/put! out message/full-sync-request) + shadow) + (let [state (sync/get-current-state states*)] + (a/put! out (message/full-sync state)) + state))))) + +(defmethod handle-message :full-sync-request [msg out states shadow client?] + (let [state (sync/get-current-state @states)] + (a/put! out (message/full-sync state)) + state)) + +(defmethod handle-message :full-sync [msg out states shadow client?] + (let [state (:state msg)] + (reset! states (sync/new-state state)) + state)) + +(defmethod handle-message :new-state [msg out states shadow client?] + (let [{:keys [diff hash]} (sync/diff-states (:state msg) shadow)] + (when-not (sync/empty-diff? diff) + (a/put! out (message/diff-msg diff hash))) + shadow)) + (defn make-agent ([client? in out states] (make-agent client? in out states sync/empty-state)) ([client? in out states initial-shadow] (go (loop [shadow initial-shadow] (when-let [msg (! out (message/diff-msg diff hash))) - (if client? - (recur new-shadow) - (recur (sync/get-current-state new-states)))) - (if client? - (do (>! out message/full-sync-request) - (recur shadow)) - (let [state (sync/get-current-state states*)] - (>! out (message/full-sync state)) - (recur state))))) - - :full-sync-request - (let [state (sync/get-current-state @states)] - (>! out (message/full-sync state)) - (recur state)) - - :full-sync - (let [state (:state msg)] - (reset! states (sync/new-state state)) - (recur state)) - - :new-state - (let [{:keys [diff hash]} (sync/diff-states (:state msg) shadow)] - (when-not (sync/empty-diff? diff) - (>! out (message/diff-msg diff hash))) - (recur shadow)) - (recur shadow))))))) + (recur (handle-message msg out states shadow client?))))))) (defn make-server-agent ([in out states] (make-agent false in out states))