 (defmethod handle-message :frontend/update-subscriber [app-state message data]
   (swap! app-state update-in [:subscribers (:client-uuid data)] merge (:subscriber-data data)))
 
+;; These are a little bit different, we're putting the message on the channel in a send-msg callback
+(defmethod handle-message :frontend/subscribe [app-state message data]
+  (let [{:keys [document layers chats client-uuid]} data]
+    ;; TODO: if this is a good idea, then make it the default
+    (put! (get-in @app-state [:comms :controls])
+          [:show-mouse-toggled {:client-uuid client-uuid
+                                :show-mouse? true}])
+    (d/transact! (:db @app-state)
+                 (concat layers chats)
+                 {:server-update true})))
+
+(defmethod handle-message :frontend/fetch-subscribers [app-state message {:keys [subscribers]}]
+  (swap! app-state update-in [:subscribers] (fn [s]
+                                              (merge-with merge
+                                                          subscribers
+                                                          s))))
+
+
 (defn do-something [app-state sente-state]
   (let [tap (async/chan (async/sliding-buffer 10))
         mult (:ch-recv-mult sente-state)]

(defn init [app-state]
  (let [{:keys [chsk ch-recv send-fn state] :as sente-state} (sente/make-channel-socket! "/chsk" {:type :auto})]
    (swap! app-state assoc :sente (assoc sente-state :ch-recv-mult (async/mult ch-recv)))
    (subscribe-to-document (:sente @app-state) app-state (-> @app-state :document/id))
    (fetch-subscribers (:sente @app-state) app-state (-> @app-state :document/id))
    (do-something app-state (:sente @app-state))))

(defn send-msg [sente-state message & [timeout-ms callback-fn :as rest]]
  (if (-> sente-state :state deref :open?)
    (apply (:send-fn sente-state) message rest)
    (let [watch-id (utils/uuid)]
      (add-watch (:state sente-state) watch-id
                 (fn [key ref old new]
                   (when (:open? new)
                     (apply (:send-fn sente-state) message rest)
                     (remove-watch ref watch-id)))))))

(defn subscribe-to-document [sente-state document-id]
  (send-msg sente-state [:frontend/subscribe {:document-id document-id}] 10000
            (fn [data]
              (put! (:ch-recv sente-state) [:chsk/recv [:frontend/subscribe data]]))))

(defn fetch-subscribers [sente-state document-id]
  (send-msg sente-state [:frontend/fetch-subscribers {:document-id document-id}] 10000
            (fn [data]
              (put! (:ch-recv sente-state) [:chsk/recv [:frontend/fetch-subscribers data]]))))

(defmulti handle-message (fn [app-state message data]
                           (utils/mlog "handle-message" message data)
