Harlan Iverson 8 роки тому
батько
коміт
5456fd2376
1 змінених файлів з 47 додано та 7 видалено
  1. 47 7
      web-client/src/example/core.cljs

+ 47 - 7
web-client/src/example/core.cljs

@@ -16,29 +16,69 @@
     (.info js/console "App stopped.")
     component))
 
-(defrecord Sente [client-id
+(defprotocol Logger
+  (info [c msg args])
+  (error [c msg args])
+  (debug [c msg args]))
+
+(defn- send-log! [c event msg args]
+  (let [send-fn! (get-in c [:sente-chsk :send-fn])]
+    (.info js/console (str event ": " msg))
+    (send-fn! [event {:log/msg msg
+                      :log/args args}])))
+
+
+(defrecord PubSub []
+  component/Lifecycle
+  (start [c]
+    (comment let [local-pub (chan)
+          local-pubsub {:pub local-pub
+                        :subs (subs pub)}
+          dispatch (comment fn [event]
+                     (when (remote-dispatch? event))
+                     )])
+    c)
+  (stop [c] c))
+
+(defrecord Sente [env
                   sente-chsk]
   component/Lifecycle
   (start [c]
     (let [sente-chsk (sente/make-channel-socket! "/chsk" {:type :auto
-                                                          :client-id client-id})]
+                                                          :client-id (:client-id env)})
+          c (assoc c :sente-chsk sente-chsk)]
       (go (while true
             (let [event (<! (:ch-recv sente-chsk))]
               (.info js/console (str "Got event! " event)))))
-      (.info js/console "Sente started.")
+      (.info js/console "Sente started. client-id=" (:client-id env))
 
       (.setTimeout js/window #((:send-fn sente-chsk) [:client/hi]) 500)
+      (.setTimeout js/window #(info c "HI!!!!!" ["From a timer"]) 500)
 
-      (assoc c :sente-chsk sente-chsk)))
+      c))
   (stop [c]
     (when (:sente-chsk c)
       (.info js/console "Sente stopped.")
-      (dissoc c :sente-chsk))))
+      (dissoc c :sente-chsk)))
+
+  Logger
+  (info [c msg args] (send-log! c ::log.info msg args))
+  (error [c msg args] (send-log! c ::log.error msg args))
+  (debug [c msg args] (send-log! c ::log.debug msg args)))
+
+
+(defrecord Env [client-id])
 
-(defonce system (atom (component/system-map :sente (map->Sente {:client-id "1234"})
+(defonce system (atom (component/system-map :env (map->Env {:client-id "1234"})
+                                            :sente (component/using
+                                                     (map->Sente {})
+                                                     [:env])
+                                            :pubsub (map->PubSub {})
                                             :app (component/using
                                                    (map->App {})
-                                                   {:sente :sente}))))
+                                                   {:env :env
+                                                    :sente :sente
+                                                    :pubsub :pubsub}))))
 
 (defn send-message [msg]
   (let [sente-chsk (get-in @system [:sente :sente-chsk])]