Преглед на файлове

enhance(rtc): update debug ui

rcmerci преди 1 година
родител
ревизия
d84f0a6d82
променени са 3 файла, в които са добавени 40 реда и са изтрити 11 реда
  1. 6 4
      src/main/frontend/db/rtc/debug_ui.cljs
  2. 19 1
      src/main/frontend/worker/rtc/client.cljs
  3. 15 6
      src/main/frontend/worker/rtc/core.cljs

+ 6 - 4
src/main/frontend/db/rtc/debug_ui.cljs

@@ -24,7 +24,9 @@
   rum/reactive
   []
   (let [state (rum/react debug-state)
-        rtc-state (:rtc-state state)]
+        rtc-state (:rtc-state state)
+        rtc-lock (:rtc-lock state)
+        rtc-logs (:rtc-logs state)]
     [:div
      {:on-click (fn [^js e]
                   (when-let [^js btn (.closest (.-target e) ".ui__button")]
@@ -38,7 +40,7 @@
                       (p/let [result (.rtc-get-debug-state2 worker)
                               new-state (ldb/read-transit-str result)]
                         (swap! debug-state (fn [old] (merge old new-state))))))}
-       (shui/tabler-icon "refresh") "local-state")
+       (shui/tabler-icon "refresh") "state")
 
       (shui/button
        {:size :sm
@@ -75,6 +77,7 @@
        (-> {:user-uuid (user/user-uuid)
             :graph (:graph-uuid state)
             :rtc-state rtc-state
+            :rtc-logs rtc-logs
             :local-tx (:local-tx state)
             :pending-block-update-count (:unpushed-block-update-count state)
             :remote-graphs (:remote-graphs state)
@@ -86,8 +89,7 @@
            (fipp/pprint {:width 20})
            with-out-str)]]
 
-     (if (or (nil? rtc-state)
-             (= :closed rtc-state))
+     (if (nil? rtc-lock)
        (shui/button
         {:variant :outline
          :class "text-green-rx-09 border-green-rx-10 hover:text-green-rx-10"

+ 19 - 1
src/main/frontend/worker/rtc/client.cljs

@@ -81,7 +81,6 @@
   [db attr]
   (= :db.cardinality/many (get-in (d/schema db) [attr :db/cardinality])))
 
-
 (defn- remove-redundant-av
   "Remove previous av if later-av has same [a v] or a"
   [db av-coll]
@@ -325,3 +324,22 @@
                 (r.remote-update/apply-remote-update
                  repo conn date-formatter {:type :remote-update :value r} add-log-fn)
                 (add-log-fn {:type ::push-client-updates :remote-t (:t r)}))))))))
+
+(defn new-task--pull-remote-data
+  [repo conn graph-uuid date-formatter get-ws-create-task add-log-fn]
+  (m/sp
+    (let [local-tx (op-mem-layer/get-local-tx repo)
+          r (m/? (send&recv get-ws-create-task {:action "apply-ops" :graph-uuid graph-uuid
+                                                :ops [] :t-before (or local-tx 1)}))]
+      (if-let [remote-ex (:ex-data r)]
+        (do (add-log-fn remote-ex)
+            (case (:type remote-ex)
+              :graph-lock-failed nil
+              :graph-lock-missing (throw r.ex/ex-remote-graph-lock-missing)
+              :rtc.exception/get-s3-object-failed nil
+              ;;else
+              (throw (ex-info "Unavailable" {:remote-ex remote-ex}))))
+        (do (assert (pos? (:t r)) r)
+            (r.remote-update/apply-remote-update
+             repo conn date-formatter {:type :remote-update :value r} add-log-fn)
+            (add-log-fn {:type ::pull-remote-data :remote-t (:t r) :local-t local-tx}))))))

+ 15 - 6
src/main/frontend/worker/rtc/core.cljs

@@ -159,8 +159,12 @@
            (let [event (m/?> mixed-flow)]
              (case (:type event)
                :remote-update
-               (r.remote-update/apply-remote-update repo conn date-formatter event add-log-fn)
-               ;; TODO: add case: fetch earlier remote data
+               (try (r.remote-update/apply-remote-update repo conn date-formatter event add-log-fn)
+                    (catch :default e
+                      (when (= ::r.remote-update/need-pull-remote-data (:type (ex-data e)))
+                        (m/? (r.client/new-task--pull-remote-data
+                              repo conn graph-uuid date-formatter get-ws-create-task add-log-fn)))))
+
                :local-update-check
                (m/? (r.client/new-task--push-local-ops
                      repo conn graph-uuid date-formatter
@@ -178,6 +182,7 @@
    :rtc-log-flow nil
    :rtc-state-flow nil
    :*rtc-auto-push? nil
+   :*rtc-lock nil
    :canceler nil})
 
 (defonce ^:private *rtc-loop-metadata (atom empty-rtc-loop-metadata))
@@ -203,6 +208,7 @@
                                             :rtc-log-flow rtc-log-flow
                                             :rtc-state-flow rtc-state-flow
                                             :*rtc-auto-push? *rtc-auto-push?
+                                            :*rtc-lock *rtc-lock
                                             :canceler canceler})
                 nil)))
         (r.ex/->map r.ex/ex-local-not-rtc-graph))
@@ -267,19 +273,22 @@
   []
   (let [rtc-loop-metadata-flow (m/watch *rtc-loop-metadata)]
     (m/ap
-      (let [{:keys [repo graph-uuid user-uuid rtc-state-flow *rtc-auto-push?]}
+      (let [{:keys [repo graph-uuid user-uuid rtc-state-flow *rtc-auto-push? *rtc-lock rtc-log-flow]}
             (m/?< rtc-loop-metadata-flow)]
         (try
-          (when (and repo rtc-state-flow *rtc-auto-push?)
+          (when (and repo rtc-state-flow *rtc-auto-push? *rtc-lock rtc-log-flow)
             (m/?<
              (m/latest
-              (fn [rtc-state rtc-auto-push?]
+              (fn [rtc-state rtc-auto-push? rtc-lock rtc-logs]
                 {:graph-uuid graph-uuid
                  :user-uuid user-uuid
                  :unpushed-block-update-count (op-mem-layer/get-unpushed-block-update-count repo)
                  :rtc-state rtc-state
+                 :rtc-lock rtc-lock
+                 :rtc-logs rtc-logs
                  :auto-push? rtc-auto-push?})
-              rtc-state-flow (m/watch *rtc-auto-push?))))
+              rtc-state-flow (m/watch *rtc-auto-push?) (m/watch *rtc-lock)
+              (m/reductions (fn [r log] (if log (take 10 (conj r log)) r)) nil rtc-log-flow))))
           (catch Cancelled _))))))
 
 (defn new-task--get-debug-state