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.core.async.macros :refer [go]]))
|
||||
|
||||
(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)]
|
||||
(condp = (:type msg)
|
||||
:diff
|
||||
(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
|
||||
|
@ -25,33 +21,39 @@
|
|||
(when-not (= states* new-states)
|
||||
(reset! states new-states)))
|
||||
(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?
|
||||
(recur new-shadow)
|
||||
(recur (sync/get-current-state new-states))))
|
||||
new-shadow
|
||||
(sync/get-current-state new-states)))
|
||||
(if client?
|
||||
(do (>! out message/full-sync-request)
|
||||
(recur shadow))
|
||||
(do (a/put! out message/full-sync-request)
|
||||
shadow)
|
||||
(let [state (sync/get-current-state states*)]
|
||||
(>! out (message/full-sync state))
|
||||
(recur state)))))
|
||||
(a/put! out (message/full-sync state))
|
||||
state)))))
|
||||
|
||||
:full-sync-request
|
||||
(defmethod handle-message :full-sync-request [msg out states shadow client?]
|
||||
(let [state (sync/get-current-state @states)]
|
||||
(>! out (message/full-sync state))
|
||||
(recur state))
|
||||
(a/put! out (message/full-sync state))
|
||||
state))
|
||||
|
||||
:full-sync
|
||||
(defmethod handle-message :full-sync [msg out states shadow client?]
|
||||
(let [state (:state msg)]
|
||||
(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)]
|
||||
(when-not (sync/empty-diff? diff)
|
||||
(>! out (message/diff-msg diff hash)))
|
||||
(recur shadow))
|
||||
(recur shadow)))))))
|
||||
(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 (<! in)]
|
||||
(recur (handle-message msg out states shadow client?)))))))
|
||||
|
||||
(defn make-server-agent
|
||||
([in out states] (make-agent false in out states))
|
||||
|
|
Loading…
Reference in a new issue