Separate event handling into handler methods
This commit is contained in:
parent
52ce9ffb42
commit
bef0e8d4e7
1 changed files with 42 additions and 40 deletions
|
@ -7,13 +7,9 @@
|
||||||
#+cljs (:require-macros [grub.macros :refer [log logs]]
|
#+cljs (:require-macros [grub.macros :refer [log logs]]
|
||||||
[cljs.core.async.macros :refer [go]]))
|
[cljs.core.async.macros :refer [go]]))
|
||||||
|
|
||||||
(defn make-agent
|
(defmulti handle-message (fn [msg out states shadow client?] (:type msg)))
|
||||||
([client? in out states] (make-agent client? in out states sync/empty-state))
|
|
||||||
([client? in out states initial-shadow]
|
(defmethod handle-message :diff [msg out states shadow client?]
|
||||||
(go (loop [shadow initial-shadow]
|
|
||||||
(when-let [msg (<! in)]
|
|
||||||
(condp = (:type msg)
|
|
||||||
:diff
|
|
||||||
(let [states* @states
|
(let [states* @states
|
||||||
shadow (sync/get-history-state states* (:hash msg))]
|
shadow (sync/get-history-state states* (:hash msg))]
|
||||||
(if shadow
|
(if shadow
|
||||||
|
@ -25,33 +21,39 @@
|
||||||
(when-not (= states* new-states)
|
(when-not (= states* new-states)
|
||||||
(reset! states new-states)))
|
(reset! states new-states)))
|
||||||
(when-not (or client? (sync/empty-diff? (:diff msg)))
|
(when-not (or client? (sync/empty-diff? (:diff msg)))
|
||||||
(>! out (message/diff-msg diff hash)))
|
(a/put! out (message/diff-msg diff hash)))
|
||||||
(if client?
|
(if client?
|
||||||
(recur new-shadow)
|
new-shadow
|
||||||
(recur (sync/get-current-state new-states))))
|
(sync/get-current-state new-states)))
|
||||||
(if client?
|
(if client?
|
||||||
(do (>! out message/full-sync-request)
|
(do (a/put! out message/full-sync-request)
|
||||||
(recur shadow))
|
shadow)
|
||||||
(let [state (sync/get-current-state states*)]
|
(let [state (sync/get-current-state states*)]
|
||||||
(>! out (message/full-sync state))
|
(a/put! out (message/full-sync state))
|
||||||
(recur state)))))
|
state)))))
|
||||||
|
|
||||||
:full-sync-request
|
(defmethod handle-message :full-sync-request [msg out states shadow client?]
|
||||||
(let [state (sync/get-current-state @states)]
|
(let [state (sync/get-current-state @states)]
|
||||||
(>! out (message/full-sync state))
|
(a/put! out (message/full-sync state))
|
||||||
(recur state))
|
state))
|
||||||
|
|
||||||
:full-sync
|
(defmethod handle-message :full-sync [msg out states shadow client?]
|
||||||
(let [state (:state msg)]
|
(let [state (:state msg)]
|
||||||
(reset! states (sync/new-state state))
|
(reset! states (sync/new-state state))
|
||||||
(recur state))
|
state))
|
||||||
|
|
||||||
:new-state
|
(defmethod handle-message :new-state [msg out states shadow client?]
|
||||||
(let [{:keys [diff hash]} (sync/diff-states (:state msg) shadow)]
|
(let [{:keys [diff hash]} (sync/diff-states (:state msg) shadow)]
|
||||||
(when-not (sync/empty-diff? diff)
|
(when-not (sync/empty-diff? diff)
|
||||||
(>! out (message/diff-msg diff hash)))
|
(a/put! out (message/diff-msg diff hash)))
|
||||||
(recur shadow))
|
shadow))
|
||||||
(recur 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 (<! in)]
|
||||||
|
(recur (handle-message msg out states shadow client?)))))))
|
||||||
|
|
||||||
(defn make-server-agent
|
(defn make-server-agent
|
||||||
([in out states] (make-agent false in out states))
|
([in out states] (make-agent false in out states))
|
||||||
|
|
Loading…
Reference in a new issue