Simplify state functions
This commit is contained in:
parent
5fd54b62e6
commit
8f5814e07e
1 changed files with 13 additions and 20 deletions
|
@ -51,9 +51,7 @@
|
||||||
{:new-states (sync/add-history-state states state)
|
{:new-states (sync/add-history-state states state)
|
||||||
:out-event (when-not (sync/empty-diff? diff) (message/diff-msg diff hash))}))
|
:out-event (when-not (sync/empty-diff? diff) (message/diff-msg diff hash))}))
|
||||||
|
|
||||||
(defn make-agent
|
(defn make-agent [client? <remote >remote states* initial-shadow]
|
||||||
([client? <remote >remote states*] (make-agent client? <remote >remote states* sync/empty-state))
|
|
||||||
([client? <remote >remote states* initial-shadow]
|
|
||||||
(go (loop [shadow initial-shadow]
|
(go (loop [shadow initial-shadow]
|
||||||
(when-let [msg (<! <remote)]
|
(when-let [msg (<! <remote)]
|
||||||
(let [states @states*
|
(let [states @states*
|
||||||
|
@ -61,15 +59,10 @@
|
||||||
{:keys [new-states new-shadow out-event]} (handle-event event)]
|
{:keys [new-states new-shadow out-event]} (handle-event event)]
|
||||||
(when (and new-states (not= states new-states)) (reset! states* new-states))
|
(when (and new-states (not= states new-states)) (reset! states* new-states))
|
||||||
(when out-event (a/put! >remote out-event))
|
(when out-event (a/put! >remote out-event))
|
||||||
(recur (if new-shadow new-shadow shadow))))))))
|
(recur (if new-shadow new-shadow shadow)))))))
|
||||||
|
|
||||||
(defn make-server-agent
|
(def make-server-agent (partial make-agent false))
|
||||||
([<remote >remote states] (make-agent false <remote >remote states))
|
(def make-client-agent (partial make-agent true))
|
||||||
([<remote >remote states initial-shadow] (make-agent false <remote >remote states initial-shadow)))
|
|
||||||
|
|
||||||
(defn make-client-agent
|
|
||||||
([<remote >remote states] (make-agent true <remote >remote states))
|
|
||||||
([<remote >remote states initial-shadow] (make-agent true <remote >remote states initial-shadow)))
|
|
||||||
|
|
||||||
#+clj
|
#+clj
|
||||||
(defn sync-new-client! [>client <client states]
|
(defn sync-new-client! [>client <client states]
|
||||||
|
@ -87,7 +80,7 @@
|
||||||
(do (remove-watch states client-id)
|
(do (remove-watch states client-id)
|
||||||
(a/close! <client)
|
(a/close! <client)
|
||||||
(a/close! state-change-events)))))
|
(a/close! state-change-events)))))
|
||||||
(make-server-agent client-events >client states)))
|
(make-server-agent client-events >client states sync/empty-state)))
|
||||||
|
|
||||||
#+clj
|
#+clj
|
||||||
(defn init-server [to-db initial-state]
|
(defn init-server [to-db initial-state]
|
||||||
|
@ -104,6 +97,6 @@
|
||||||
(let [new-state (sync/get-current-state new-states)]
|
(let [new-state (sync/get-current-state new-states)]
|
||||||
(a/put! >view new-state))))
|
(a/put! >view new-state))))
|
||||||
(a/pipe <view local-events)
|
(a/pipe <view local-events)
|
||||||
(make-client-agent (a/merge [local-events <remote]) >remote states)
|
(make-client-agent (a/merge [local-events <remote]) >remote states sync/empty-state)
|
||||||
(a/put! >remote message/full-sync-request)
|
(a/put! >remote message/full-sync-request)
|
||||||
states))
|
states))
|
||||||
|
|
Loading…
Reference in a new issue