Browse Source

enhance(rtc): stop rtc when logout or graph-switch

rcmerci 8 months ago
parent
commit
ef7c688c1b

+ 1 - 0
src/main/frontend/core.cljs

@@ -7,6 +7,7 @@
             [frontend.config :as config]
             [frontend.fs.sync :as sync]
             [frontend.handler :as handler]
+            [frontend.handler.db-based.rtc-background-tasks]
             [frontend.handler.plugin :as plugin-handler]
             [frontend.handler.route :as route-handler]
             [frontend.log]

+ 4 - 54
src/main/frontend/handler/db_based/rtc.cljs

@@ -1,22 +1,15 @@
 (ns frontend.handler.db-based.rtc
   "RTC handler"
-  (:require [cljs-time.core :as t]
-            [clojure.pprint :as pp]
-            [frontend.common.missionary :as c.m]
+  (:require [clojure.pprint :as pp]
             [frontend.config :as config]
             [frontend.db :as db]
-            [frontend.flows :as flows]
-            [frontend.handler.db-based.rtc-flows :as rtc-flows]
             [frontend.handler.notification :as notification]
             [frontend.handler.user :as user-handler]
             [frontend.state :as state]
             [frontend.util :as util]
-            [lambdaisland.glogi :as log]
-            [logseq.common.util :as common-util]
             [logseq.db :as ldb]
             [logseq.db.common.sqlite :as sqlite-common-db]
             [logseq.shui.ui :as shui]
-            [missionary.core :as m]
             [promesa.core :as p]))
 
 (defn <rtc-create-graph!
@@ -75,7 +68,7 @@
       (when-let [ex-data* (:ex-data start-ex)]
         (throw (ex-info (:ex-message start-ex) ex-data*))))))
 
-(defn- notification-download-higher-schema-graph!
+(defn notification-download-higher-schema-graph!
   [graph-name graph-uuid schema-version]
   (let [graph-name* (str graph-name "-" schema-version)]
     (notification/show!
@@ -180,50 +173,7 @@
         (.rtc-grant-graph-access worker token (str graph-uuid)
                                  (ldb/write-transit-str [])
                                  (ldb/write-transit-str [email]))
-        (notification/show! (str "Invitation sent!") :success))
+        (notification/show! "Invitation sent!" :success))
        (p/catch (fn [e]
-                  (notification/show! (str "Something wrong, please try again.") :error)
+                  (notification/show! "Something wrong, please try again." :error)
                   (js/console.error e)))))))
-
-(when-not config/publishing?
-  (c.m/run-background-task
-   ;;; background task: try to restart rtc-loop when possible,
-   ;;; triggered by `rtc-flows/rtc-try-restart-flow`
-   ::restart-rtc-task
-   (m/reduce
-    (constantly nil)
-    (m/ap
-      (let [{:keys [graph-uuid t]} (m/?> rtc-flows/rtc-try-restart-flow)]
-        (when (and graph-uuid t
-                   (= graph-uuid (ldb/get-graph-rtc-uuid (db/get-db)))
-                   (> 5000 (- (common-util/time-ms) t)))
-          (prn :trying-to-restart-rtc graph-uuid (t/now))
-          (c.m/<? (<rtc-start! (state/get-current-repo) :stop-before-start? false)))))))
-
-  (c.m/run-background-task
-   ::notify-client-need-upgrade-when-larger-remote-schema-version-exists
-   (m/reduce
-    (constantly nil)
-    (m/ap
-      (let [{:keys [repo graph-uuid remote-schema-version sub-type]}
-            (m/?>
-             (m/eduction
-              (filter #(keyword-identical? :rtc.log/higher-remote-schema-version-exists (:type %)))
-              rtc-flows/rtc-log-flow))]
-        (case sub-type
-          :download
-          (notification-download-higher-schema-graph! repo graph-uuid remote-schema-version)
-          ;; else
-          (notification/show!
-           "The server has a graph with a higher schema version, the client may need to upgrade."
-           :warning))))))
-
-  (c.m/run-background-task
-   ::stop-rtc-when-user-logout
-   (m/reduce
-    (constantly nil)
-    (m/ap
-      (let [login-user (m/?> flows/current-login-user-flow)]
-        (when (= :logout login-user)
-          (log/info :try-to-stop-rtc-when-user-logout nil)
-          (c.m/<? (<rtc-stop!))))))))

+ 71 - 0
src/main/frontend/handler/db_based/rtc_background_tasks.cljs

@@ -0,0 +1,71 @@
+(ns frontend.handler.db-based.rtc-background-tasks
+  "Background tasks related to RTC"
+  (:require [cljs-time.core :as t]
+            [frontend.common.missionary :as c.m]
+            [frontend.config :as config]
+            [frontend.db :as db]
+            [frontend.flows :as flows]
+            [frontend.handler.db-based.rtc :as rtc-handler]
+            [frontend.handler.db-based.rtc-flows :as rtc-flows]
+            [frontend.handler.notification :as notification]
+            [frontend.state :as state]
+            [lambdaisland.glogi :as log]
+            [logseq.common.util :as common-util]
+            [logseq.db :as ldb]
+            [missionary.core :as m]))
+
+(defn- run-background-task-when-not-publishing
+  [key' task]
+  (when-not config/publishing?
+    (c.m/run-background-task key' task)))
+
+(run-background-task-when-not-publishing
+ ;; try to restart rtc-loop when possible,
+ ;; triggered by `rtc-flows/rtc-try-restart-flow`
+ ::restart-rtc-task
+ (m/reduce
+  (constantly nil)
+  (m/ap
+    (let [{:keys [graph-uuid t]} (m/?> rtc-flows/rtc-try-restart-flow)]
+      (when (and graph-uuid t
+                 (= graph-uuid (ldb/get-graph-rtc-uuid (db/get-db)))
+                 (> 5000 (- (common-util/time-ms) t)))
+        (log/info :trying-to-restart-rtc graph-uuid :t (t/now))
+        (c.m/<? (rtc-handler/<rtc-start! (state/get-current-repo) :stop-before-start? false)))))))
+
+(run-background-task-when-not-publishing
+ ::notify-client-need-upgrade-when-larger-remote-schema-version-exists
+ (m/reduce
+  (constantly nil)
+  (m/ap
+    (let [{:keys [repo graph-uuid remote-schema-version sub-type]}
+          (m/?>
+           (m/eduction
+            (filter #(keyword-identical? :rtc.log/higher-remote-schema-version-exists (:type %)))
+            rtc-flows/rtc-log-flow))]
+      (case sub-type
+        :download
+        (rtc-handler/notification-download-higher-schema-graph! repo graph-uuid remote-schema-version)
+         ;; else
+        (notification/show!
+         "The server has a graph with a higher schema version, the client may need to upgrade."
+         :warning))))))
+
+(def ^:private logout-or-graph-switch-flow
+  (c.m/mix
+   (m/eduction
+    (filter #(= :logout %))
+    flows/current-login-user-flow)
+   (m/eduction
+    (keep (fn [x] (when x :graph-switch)))
+    flows/current-repo-flow)))
+
+(run-background-task-when-not-publishing
+ ;; stop rtc when [graph-switch user-logout]
+ ::stop-rtc-if-needed
+ (m/reduce
+  (constantly nil)
+  (m/ap
+    (let [logout-or-graph-switch (m/?> logout-or-graph-switch-flow)]
+      (log/info :try-to-stop-rtc-if-needed logout-or-graph-switch)
+      (c.m/<? (rtc-handler/<rtc-stop!))))))