Return "out" event instead of directly pushing out event

This commit is contained in:
Nicholas Kariniemi 2014-10-10 20:03:53 +03:00
parent 63153ecc5d
commit 14427e89d9

View file

@ -9,7 +9,7 @@
(defmulti handle-event (fn [event] (:type event))) (defmulti handle-event (fn [event] (:type event)))
(defmethod handle-event :diff [{:keys [hash diff >remote states shadow client?] :as msg}] (defmethod handle-event :diff [{:keys [hash diff states shadow client?] :as msg}]
(let [shadow (sync/get-history-state states hash)] (let [shadow (sync/get-history-state states hash)]
(if shadow (if shadow
(let [new-states (sync/apply-diff states diff) (let [new-states (sync/apply-diff states diff)
@ -18,46 +18,42 @@
(if client? (if client?
{:new-states (sync/new-state (sync/get-current-state new-states)) {:new-states (sync/new-state (sync/get-current-state new-states))
:new-shadow new-shadow} :new-shadow new-shadow}
(do (when-not (sync/empty-diff? diff) {:out-event (when-not (sync/empty-diff? diff)
(a/put! >remote (message/diff-msg new-diff new-hash))) (message/diff-msg new-diff new-hash))
{:new-states new-states :new-states new-states
:new-shadow (sync/get-current-state new-states)}))) :new-shadow (sync/get-current-state new-states)}))
(if client? (if client?
(do (a/put! >remote message/full-sync-request) {:out-event message/full-sync-request
{:new-shadow shadow}) :new-shadow shadow}
(let [state (sync/get-current-state states)] (let [state (sync/get-current-state states)]
(a/put! >remote (message/full-sync state)) {:out-event (message/full-sync state)
{:new-shadow state}))))) :new-shadow state})))))
(defmethod handle-event :full-sync-request [{:keys [states >remote]}] (defmethod handle-event :full-sync-request [{:keys [states]}]
(let [state (sync/get-current-state states)] (let [state (sync/get-current-state states)]
(a/put! >remote (message/full-sync state)) {:new-shadow state
{:new-shadow state})) :out-event (message/full-sync state)}))
(defmethod handle-event :full-sync [{:keys [state states]}] (defmethod handle-event :full-sync [{:keys [state states]}]
{:new-states (sync/new-state state) {:new-states (sync/new-state state)
:new-shadow state}) :new-shadow state})
(defmethod handle-event :new-state [{:keys [state states shadow >remote]}] (defmethod handle-event :new-state [{:keys [state states shadow]}]
(let [{:keys [diff hash]} (sync/diff-states state shadow)] (let [{:keys [diff hash]} (sync/diff-states state shadow)]
(when-not (sync/empty-diff? diff) {:new-shadow shadow
(a/put! >remote (message/diff-msg diff hash))) :out-event (when-not (sync/empty-diff? diff) (message/diff-msg diff hash))}))
{:new-shadow shadow}))
(defn make-agent (defn make-agent
([client? <remote >remote states*] (make-agent client? <remote >remote states* sync/empty-state)) ([client? <remote >remote states*] (make-agent client? <remote >remote states* sync/empty-state))
([client? <remote >remote states* initial-shadow] ([client? <remote >remote states* initial-shadow]
(let [msg->event (fn [msg states shadow] (go (loop [shadow initial-shadow]
(assoc msg (when-let [msg (<! <remote)]
:>remote >remote :states states (let [states @states*
:client? client? :shadow shadow))] event (assoc msg :states states :client? client? :shadow shadow)
(go (loop [shadow initial-shadow] {:keys [new-states new-shadow out-event]} (handle-event event)]
(when-let [msg (<! <remote)] (when (and new-states (not= states new-states)) (reset! states* new-states))
(let [states @states* (when out-event (a/put! >remote out-event))
event (msg->event msg states shadow) (recur shadow)))))))
{:keys [new-states new-shadow]} (handle-event event)]
(when (and new-states (not= states new-states)) (reset! states* new-states))
(recur shadow))))))))
(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))