                                              2 42
                                              3 43}})
            (recur (<! in))))
+
+
+(defn fake-peer [store]
+  (let [fake-id (str "FAKE" (rand-int 100))
+        in (chan) #_(print-chan (str fake-id "-IN"))
+        out (async/pub in (fn [{:keys [user meta]}] [user (:id meta)]))
+        #_(print-chan (str fake-id "-OUT"))]
+    {:volatile {:server nil
+                :chans [in out]
+                :store store}
+     :ip fake-id}))
 
 
 (defn realize-value [stage store eval-fn]



(defn sync! [peer stage]
  (go (let [{:keys [type author chans new-values meta]} (<! (ensure-conn peer stage))
            [in out] chans
            p (async/pub in :type)
            fch (chan)]

        (case type
          :meta-up (>! out {:type :meta-up :user author :meta meta})
          :new-meta (do
                      (>! out {:type :new-meta :user author :repo (:id meta)})
                      (>! out {:type :meta-up :user author :meta meta})))

        (async/sub p :fetch fch)
        (let [to-fetch (select-keys new-values (:ids (<! fch)))]
          (>! out {:type :fetched
                   :fetched to-fetch}))

        (dissoc stage :type :new-values))))


#_(let [p (fake-peer mem-store)]
    (go  (<! (sync!
                       p
                       (repo/new-repository "me@mail.com"
                                            {:type "s" :version 1}
                                            "Testing."
                                            false
                                            {:some 42})))
      (println "pubz:" (<! (first (:chans (:volatile p)))))))


(defn fake-peer [store]
  (let [fake-id (str "FAKE" (rand-int 100))
        in (print-chan (str fake-id "-IN"))
        out (print-chan (str fake-id "-OUT")
                        (async/pub in (fn [{:keys [user id]}] [user id])))]
    {:volatile {:server nil
                :chans [in out]
                :store store}
     :ip fake-id}))


#_(let [in (chan)
      out (chan)
      peer (fake-peer mem-store)
      p (async/onto-chan out [{:type :new-meta

                               :user "john" :repo 1}
                              #_{:type :publish
                                 :user "maria" :repo 3 :meta {:win :ning}}
                              #_{:type :subscribe
                                 :user "maria" :repo 3}
                              {:type :meta-up
                               :user "john" :meta {:id 1
                                                   :causal-order {1 #{}
                                                                  2 #{1}}
                                                   :last-update (java.util.Date.)}}]
                         false)]
  (go (wire peer [in out]))

  (go-loop [i (<! in)]
           (println "received:" i)

(defn stop [state]
  ((get-in @state [:volatile :server])))


#_(stop peer-a)

