Sfoglia il codice sorgente

refactor: update new-task--rtc-start

allow it to be used conveniently when switching the master-client in a multi-tabs environment
rcmerci 6 mesi fa
parent
commit
780d40f05c

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

@@ -111,9 +111,7 @@
        (shui/button
         {:variant :outline
          :class "text-green-rx-09 border-green-rx-10 hover:text-green-rx-10"
-         :on-click (fn []
-                     (let [token (state/get-auth-id-token)]
-                       (state/<invoke-db-worker :thread-api/rtc-start (state/get-current-repo) token)))}
+         :on-click (fn [] (state/<invoke-db-worker :thread-api/rtc-start false))}
         (shui/tabler-icon "player-play") "start")
 
        [:div.my-2.flex

+ 26 - 28
src/main/frontend/handler/db_based/rtc.cljs

@@ -96,37 +96,35 @@
   (when-let [graph-uuid (ldb/get-graph-rtc-uuid (db/get-db repo))]
     (p/do!
      (js/Promise. user-handler/task--ensure-id&access-token)
-     (when stop-before-start? (<rtc-stop!))
-     (let [token (state/get-auth-id-token)]
-       (p/let [start-ex (state/<invoke-db-worker :thread-api/rtc-start repo token)
-               ex-data* (:ex-data start-ex)
-               _ (case (:type ex-data*)
-                   (:rtc.exception/not-rtc-graph
-                    :rtc.exception/not-found-db-conn)
-                   (notification/show! (:ex-message start-ex) :error)
+     (p/let [start-ex (state/<invoke-db-worker :thread-api/rtc-start stop-before-start?)
+             ex-data* (:ex-data start-ex)
+             _ (case (:type ex-data*)
+                 (:rtc.exception/not-rtc-graph
+                  :rtc.exception/not-found-db-conn)
+                 (notification/show! (:ex-message start-ex) :error)
 
-                   :rtc.exception/major-schema-version-mismatched
-                   (case (:sub-type ex-data*)
-                     :download
-                     (notification-download-higher-schema-graph! repo graph-uuid (:remote ex-data*))
-                     :create-branch
-                     (notification-upload-higher-schema-graph! repo)
-                        ;; else
-                     (do (log/info :start-ex start-ex)
-                         (notification/show! [:div
-                                              [:div (:ex-message start-ex)]
-                                              [:div (-> ex-data*
-                                                        (select-keys [:app :local :remote])
-                                                        pp/pprint
-                                                        with-out-str)]]
-                                             :error)))
+                 :rtc.exception/major-schema-version-mismatched
+                 (case (:sub-type ex-data*)
+                   :download
+                   (notification-download-higher-schema-graph! repo graph-uuid (:remote ex-data*))
+                   :create-branch
+                   (notification-upload-higher-schema-graph! repo)
+                   ;; else
+                   (do (log/info :start-ex start-ex)
+                       (notification/show! [:div
+                                            [:div (:ex-message start-ex)]
+                                            [:div (-> ex-data*
+                                                      (select-keys [:app :local :remote])
+                                                      pp/pprint
+                                                      with-out-str)]]
+                                           :error)))
 
-                   :rtc.exception/lock-failed
-                   (js/setTimeout #(<rtc-start! repo) 1000)
+                 :rtc.exception/lock-failed
+                 (js/setTimeout #(<rtc-start! repo) 1000)
 
-                      ;; else
-                   nil)]
-         nil)))))
+                 ;; else
+                 nil)]
+       nil))))
 
 (defn <get-remote-graphs
   []

+ 37 - 13
src/main/frontend/worker/rtc/core.cljs

@@ -337,7 +337,7 @@
                                   :repo repo})))
 
 ;;; ================ API ================
-(defn new-task--rtc-start
+(defn- new-task--rtc-start*
   [repo token]
   (m/sp
     ;; ensure device metadata existing first
@@ -345,7 +345,7 @@
     (let [{:keys [conn user-uuid graph-uuid schema-version remote-schema-version date-formatter] :as r}
           (validate-rtc-start-conditions repo token)]
       (if (instance? ExceptionInfo r)
-        (do (log/info :e r) (r.ex/->map r))
+        r
         (let [{:keys [rtc-state-flow *rtc-auto-push? *rtc-remote-profile? rtc-loop-task *online-users onstarted-task]}
               (create-rtc-loop graph-uuid schema-version repo conn date-formatter token)
               *last-stop-exception (atom nil)
@@ -355,8 +355,8 @@
                                  (reset! *last-stop-exception e)
                                  (log/info :rtc-loop-task e)))
               start-ex (m/? onstarted-task)]
-          (if-let [start-ex (:ex-data start-ex)]
-            (do (log/info :start-ex start-ex) (r.ex/->map start-ex))
+          (if (instance? ExceptionInfo start-ex)
+            start-ex
             (do (reset! *rtc-loop-metadata {:repo repo
                                             :graph-uuid graph-uuid
                                             :local-graph-schema-version schema-version
@@ -371,6 +371,28 @@
                                             :*last-stop-exception *last-stop-exception})
                 nil)))))))
 
+(declare rtc-stop)
+(defn new-task--rtc-start
+  [stop-before-start?]
+  (m/sp
+    (let [repo (worker-state/get-current-repo)
+          token (worker-state/get-id-token)]
+      (when (and repo token)
+        (when stop-before-start? (rtc-stop))
+        (let [ex (m/? (new-task--rtc-start* repo token))]
+          (when-let [ex-data* (ex-data ex)]
+            (case (:type ex-data*)
+              (:rtc.exception/not-rtc-graph
+               :rtc.exception/major-schema-version-mismatched
+               :rtc.exception/lock-failed)
+              (log/info :rtc-start-failed ex)
+
+              :rtc.exception/not-found-db-conn
+              (log/error :rtc-start-failed ex)
+
+              (log/error :BUG-unknown-error ex))
+            (r.ex/->map ex)))))))
+
 (defn rtc-stop
   []
   (when-let [canceler (:canceler @*rtc-loop-metadata)]
@@ -520,10 +542,11 @@
   (let [{:keys [get-ws-create-task]} (gen-get-ws-create-map--memoized (ws-util/get-ws-url token))]
     (r.upload-download/new-task--request-download-graph get-ws-create-task graph-uuid schema-version)))
 
-(defn new-task--download-info-list
-  [token graph-uuid schema-version]
-  (let [{:keys [get-ws-create-task]} (gen-get-ws-create-map--memoized (ws-util/get-ws-url token))]
-    (r.upload-download/new-task--download-info-list get-ws-create-task graph-uuid schema-version)))
+(comment
+  (defn new-task--download-info-list
+    [token graph-uuid schema-version]
+    (let [{:keys [get-ws-create-task]} (gen-get-ws-create-map--memoized (ws-util/get-ws-url token))]
+      (r.upload-download/new-task--download-info-list get-ws-create-task graph-uuid schema-version))))
 
 (defn new-task--wait-download-info-ready
   [token download-info-uuid graph-uuid schema-version timeout-ms]
@@ -534,8 +557,8 @@
 (def new-task--download-graph-from-s3 r.upload-download/new-task--download-graph-from-s3)
 
 (def-thread-api :thread-api/rtc-start
-  [repo token]
-  (new-task--rtc-start repo token))
+  [stop-before-start?]
+  (new-task--rtc-start stop-before-start?))
 
 (def-thread-api :thread-api/rtc-stop
   []
@@ -595,9 +618,10 @@
   [graph-uuid graph-name s3-url]
   (new-task--download-graph-from-s3 graph-uuid graph-name s3-url))
 
-(def-thread-api :thread-api/rtc-download-info-list
-  [token graph-uuid schema-version]
-  (new-task--download-info-list token graph-uuid schema-version))
+(comment
+  (def-thread-api :thread-api/rtc-download-info-list
+    [token graph-uuid schema-version]
+    (new-task--download-info-list token graph-uuid schema-version)))
 
 (def-thread-api :thread-api/rtc-add-migration-client-ops
   [repo server-schema-version]

+ 1 - 0
src/main/frontend/worker/state.cljs

@@ -28,6 +28,7 @@
 (defonce *state (atom {:db/latest-transact-time {}
                        :worker/context {}
 
+                       ;; TODO: refactor: Update the following 5 values using :thread-api/update-thread-atom
                        ;; FIXME: this name :config is too general
                        :config {}
                        :git/current-repo nil