Clean up state loop

This commit is contained in:
Nicholas Kariniemi 2014-10-15 11:07:55 +03:00
parent f496fb51e5
commit dd5bf7a53a

View file

@ -59,27 +59,23 @@
(go (loop [shadow initial-shadow
states (sync/new-state @state)]
(let [[v c] (a/alts! [new-states events] :priority true)]
(when v
(cond (= c new-states)
(let [current (sync/get-current-state states)]
(when-not (= shadow v)
(cond (nil? v) nil ;; drop out of loop
(= c new-states)
(do (when-not (= shadow v)
(>! >remote (diff-msg shadow v)))
(recur shadow
(if (= v current)
states
(sync/add-history-state states v))))
(= c events)
(let [event (assoc v
:states states
:client? client?
:shadow shadow)
{:keys [new-states new-shadow out-event]} (handle-event event)]
(when (and new-states (not= states new-states))
(let [new-state (sync/get-current-state new-states)]
(reset! state new-state)))
(when out-event (a/put! >remote out-event))
(recur (if new-shadow new-shadow shadow)
(if new-states new-states states))))))))))
(recur shadow (sync/add-history-state states v)))
(= c events)
(let [event (assoc v
:states states
:client? client?
:shadow shadow)
{:keys [new-states new-shadow out-event]} (handle-event event)]
(when (and new-states (not= states new-states))
(let [new-state (sync/get-current-state new-states)]
(reset! state new-state)))
(when out-event (a/put! >remote out-event))
(recur (if new-shadow new-shadow shadow)
(if new-states new-states states)))))))))
(def make-server-agent (partial make-agent false))
(def make-client-agent (partial make-agent true))