From d7708e1382ac4c099ac3831215a2787595e1c9e7 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Fri, 26 Sep 2014 11:53:19 +0300 Subject: [PATCH] Move to shared state --- src/clj/grub/core.clj | 2 +- src/cljx/grub/shared_state.cljx | 85 +++++++++++++++++++++++++++++++ src/cljx/grub/state.cljx | 82 ----------------------------- src/test/grub/test/unit/state.clj | 2 +- 4 files changed, 87 insertions(+), 84 deletions(-) create mode 100644 src/cljx/grub/shared_state.cljx delete mode 100644 src/cljx/grub/state.cljx diff --git a/src/clj/grub/core.clj b/src/clj/grub/core.clj index d892f09..5030d0f 100644 --- a/src/clj/grub/core.clj +++ b/src/clj/grub/core.clj @@ -2,7 +2,7 @@ (:require [grub.websocket :as ws] [grub.db :as db] [grub.test.integration.core :as integration-test] - [grub.state :as state] + [grub.shared-state :as state] [ring.middleware.file :as file] [ring.util.response :as resp] [compojure.core :refer [defroutes GET POST]] diff --git a/src/cljx/grub/shared_state.cljx b/src/cljx/grub/shared_state.cljx new file mode 100644 index 0000000..157e125 --- /dev/null +++ b/src/cljx/grub/shared_state.cljx @@ -0,0 +1,85 @@ +(ns grub.shared-state + (:require [grub.diff :as diff] + [grub.message :as message] + [grub.sync :as sync] + #+clj [clojure.core.async :as a :refer [! chan go]] + #+cljs [cljs.core.async :as a :refer [! chan]]) + #+cljs (:require-macros [cljs.core.async.macros :refer [go]])) + +;; Server state +(def states (atom [])) + +(defn make-server-agent + ([in out states] (make-server-agent in out states sync/empty-state)) + ([in out states initial-client-state] + (go (loop [client-state initial-client-state] + (when-let [msg (! out (message/diff-msg diff hash)) + (recur new-shadow)) + (let [state (sync/get-current-state @states)] + (>! out (message/full-sync state)) + (recur state)))) + + :full-sync + (let [state (sync/get-current-state @states)] + (>! out (message/full-sync state)) + (recur state)) + + :new-state + (let [{:keys [diff hash]} (sync/diff-states (:new-states msg) client-state)] + (>! out (message/diff-msg diff hash)) + (recur client-state)) + (recur client-state))))))) + +;; (defn make-client-agent +;; ([in out states] (make-client-agent in out states sync/empty-state)) +;; ([in out states initial-server-state] +;; (a/go-loop [server-state initial-server-state] +;; (when-let [msg (! out (message/full-sync state)) +;; (recur state)))) + +;; :full-sync +;; (let [state (:state msg)] +;; (reset! states [state]) +;; (recur state)) + +;; :new-state +;; (let [{:keys [diff hash]} (sync/diff-states (:new-states msg) server-state)] +;; (>! out (message/diff-msg diff hash))) +;; (recur server-state)))))) + +;; TODO: Remove watch, close up channels properly +(defn sync-new-client! [>client client states))) + +(defn init [to-db grubs recipes] + (reset! states (sync/initial-state grubs recipes)) + (add-watch states :to-db (fn [_ _ old-states new-states] + (a/put! to-db (sync/get-current-state new-states))))) diff --git a/src/cljx/grub/state.cljx b/src/cljx/grub/state.cljx deleted file mode 100644 index f4921e7..0000000 --- a/src/cljx/grub/state.cljx +++ /dev/null @@ -1,82 +0,0 @@ -(ns grub.state - (:require [grub.diff :as diff] - [grub.message :as message] - [grub.sync :as sync] - [clojure.core.async :as a :refer [! chan go]])) - -;; Server state -(def states (atom [])) - -(defn make-server-agent - ([in out states] (make-server-agent in out states sync/empty-state)) - ([in out states initial-client-state] - (a/go-loop [client-state initial-client-state] - (when-let [msg (! out (message/diff-msg diff hash)) - (recur new-shadow)) - (let [state (sync/get-current-state @states)] - (>! out (message/full-sync state)) - (recur state)))) - - :full-sync - (let [state (sync/get-current-state @states)] - (>! out (message/full-sync state)) - (recur state)) - - :new-state - (let [{:keys [diff hash]} (sync/diff-states (:new-states msg) client-state)] - (>! out (message/diff-msg diff hash))) - (recur client-state)))))) - -(defn make-client-agent - ([in out states] (make-client-agent in out states sync/empty-state)) - ([in out states initial-server-state] - (a/go-loop [server-state initial-server-state] - (when-let [msg (! out (message/full-sync state)) - (recur state)))) - - :full-sync - (let [state (:state msg)] - (reset! states [state]) - (recur state)) - - :new-state - (let [{:keys [diff hash]} (sync/diff-states (:new-states msg) server-state)] - (>! out (message/diff-msg diff hash))) - (recur server-state)))))) - -;; TODO: Remove watch, close up channels properly -(defn sync-new-client! [>client client states))) - -(defn init [to-db grubs recipes] - (reset! states (sync/initial-state grubs recipes)) - (add-watch states :to-db (fn [_ _ old-states new-states] - (a/put! to-db (sync/get-current-state new-states))))) diff --git a/src/test/grub/test/unit/state.clj b/src/test/grub/test/unit/state.clj index 186229f..8078d0e 100644 --- a/src/test/grub/test/unit/state.clj +++ b/src/test/grub/test/unit/state.clj @@ -1,5 +1,5 @@ (ns grub.test.unit.state - (:require [grub.state :as state] + (:require [grub.shared-state :as state] [clojure.test :refer :all] [hasch.core :as hasch] [clojure.core.async :as a :refer [!! chan go]]))