Browse Source

fix: reactive rtc-state in debug ui

Tienson Qin 2 years ago
parent
commit
8ba3280c5a
2 changed files with 20 additions and 14 deletions
  1. 1 2
      src/main/frontend/db/rtc/debug_ui.cljs
  2. 19 12
      src/main/frontend/worker/rtc/core.cljs

+ 1 - 2
src/main/frontend/db/rtc/debug_ui.cljs

@@ -64,11 +64,10 @@
                            (count (:block/_page (db/entity [:block/name (util/page-name-sanity-lc page)]))))}
                            (count (:block/_page (db/entity [:block/name (util/page-name-sanity-lc page)]))))}
           (fipp/pprint {:width 20})
           (fipp/pprint {:width 20})
           with-out-str)]
           with-out-str)]
-     (if (or (nil? state)
+     (if (or (nil? rtc-state)
              (= :closed rtc-state))
              (= :closed rtc-state))
        (ui/button "start" {:class "my-2"
        (ui/button "start" {:class "my-2"
                            :on-click (fn []
                            :on-click (fn []
-                                       (prn :start-rtc)
                                        (let [token (state/get-auth-id-token)
                                        (let [token (state/get-auth-id-token)
                                              ^object worker @db-browser/*worker]
                                              ^object worker @db-browser/*worker]
                                          (.rtc-start worker (state/get-current-repo) token)))})
                                          (.rtc-start worker (state/get-current-repo) token)))})

+ 19 - 12
src/main/frontend/worker/rtc/core.cljs

@@ -75,7 +75,8 @@
    [:*rtc-state :any]
    [:*rtc-state :any]
    [:toggle-auto-push-client-ops-chan :any]
    [:toggle-auto-push-client-ops-chan :any]
    [:*auto-push-client-ops? :any]
    [:*auto-push-client-ops? :any]
-   [:force-push-client-ops-chan :any]])
+   [:force-push-client-ops-chan :any]
+   [:counter :any]])
 
 
 (def state-validator
 (def state-validator
   (let [validator (m/validator state-schema)]
   (let [validator (m/validator state-schema)]
@@ -742,6 +743,8 @@
       (<! (async/timeout 2000))
       (<! (async/timeout 2000))
       (pos? (op-mem-layer/get-unpushed-block-update-count repo)))))
       (pos? (op-mem-layer/get-unpushed-block-update-count repo)))))
 
 
+(defonce *state (atom nil))
+
 (defn <loop-for-rtc
 (defn <loop-for-rtc
   [state graph-uuid repo conn date-formatter & {:keys [loop-started-ch token]}]
   [state graph-uuid repo conn date-formatter & {:keys [loop-started-ch token]}]
   {:pre [(state-validator state)
   {:pre [(state-validator state)
@@ -752,6 +755,7 @@
     (reset! (:*db-conn state) conn)
     (reset! (:*db-conn state) conn)
     (reset! (:*date-formatter state) date-formatter)
     (reset! (:*date-formatter state) date-formatter)
     (reset! (:*rtc-state state) :open)
     (reset! (:*rtc-state state) :open)
+    (swap! *state update :counter inc)
     (let [{:keys [data-from-ws-pub _client-op-update-chan]} state
     (let [{:keys [data-from-ws-pub _client-op-update-chan]} state
           push-data-from-ws-ch (chan (async/sliding-buffer 100) (map rtc-const/data-from-ws-coercer))
           push-data-from-ws-ch (chan (async/sliding-buffer 100) (map rtc-const/data-from-ws-coercer))
           stop-rtc-loop-chan (chan)
           stop-rtc-loop-chan (chan)
@@ -859,9 +863,10 @@
    :*auto-push-client-ops? (atom true :validator boolean?)
    :*auto-push-client-ops? (atom true :validator boolean?)
    :*stop-rtc-loop-chan (atom nil)
    :*stop-rtc-loop-chan (atom nil)
    :force-push-client-ops-chan (chan (async/sliding-buffer 1))
    :force-push-client-ops-chan (chan (async/sliding-buffer 1))
-   :*ws (atom ws)})
+   :*ws (atom ws)
 
 
-(defonce *state (atom nil))
+   ;; used to trigger state watch
+   :counter 0})
 
 
 (defn get-debug-state
 (defn get-debug-state
   ([repo]
   ([repo]
@@ -891,6 +896,7 @@
       (<! ws-opened-ch)
       (<! ws-opened-ch)
       (let [state (init-state ws data-from-ws-chan repo token)]
       (let [state (init-state ws data-from-ws-chan repo token)]
         (reset! *state state)
         (reset! *state state)
+        (swap! *state update :counter inc)
         state))))
         state))))
 
 
 (defn <start-rtc
 (defn <start-rtc
@@ -907,9 +913,10 @@
 
 
 (defn <stop-rtc
 (defn <stop-rtc
   []
   []
-  (when-let [state @*state]
-    (when-let [*chan (:*stop-rtc-loop-chan state)]
-     (async/close! @*chan))))
+  (when-let [chan (some-> @*state
+                           :*stop-rtc-loop-chan
+                           deref)]
+    (async/close! chan)))
 
 
 (defn <toggle-sync
 (defn <toggle-sync
   []
   []
@@ -925,14 +932,14 @@
                         (<! (ws/<send! state {:req-id (get-req-id)
                         (<! (ws/<send! state {:req-id (get-req-id)
                                               :action "list-graphs"}))
                                               :action "list-graphs"}))
                         (:graphs (<! (get-result-ch))))]
                         (:graphs (<! (get-result-ch))))]
-       (prn :debug :graph-list graph-list)
        (p/resolve! d (bean/->js graph-list))))
        (p/resolve! d (bean/->js graph-list))))
     d))
     d))
 
 
 (add-watch *state :notify-main-thread
 (add-watch *state :notify-main-thread
            (fn [_ _ old new]
            (fn [_ _ old new]
-             (let [*repo (:*repo new)
-                   new-state (get-debug-state @*repo new)
-                   old-state (get-debug-state @*repo old)]
-               (when (not= new-state old-state)
-                 (worker-util/post-message :rtc-sync-state (pr-str new-state))))))
+             (when-let [*repo (:*repo new)]
+               (let [new-state (get-debug-state @*repo new)
+                     old-state (get-debug-state @*repo old)]
+                 (when (or (not= new-state old-state)
+                           (= :open (:rtc-state new-state)))
+                   (worker-util/post-message :rtc-sync-state (pr-str new-state)))))))