enhance(rtc): add :auto-start-rtc-if-possible background-task

This commit is contained in:
rcmerci
2025-02-22 22:59:12 +08:00
committed by Tienson Qin
parent 2593595ae3
commit ce4f2c3a8f
6 changed files with 57 additions and 22 deletions

View File

@@ -151,5 +151,8 @@
(fn? chan-or-promise-or-task)
chan-or-promise-or-task
(nil? chan-or-promise-or-task)
(m/sp)
:else
(throw (ex-info "Unsupported arg" {:type (type chan-or-promise-or-task)}))))

View File

@@ -6,6 +6,7 @@
[frontend.context.i18n :refer [t]]
[frontend.db :as db]
[frontend.handler.db-based.rtc :as rtc-handler]
[frontend.handler.db-based.rtc-flows :as rtc-flows]
[frontend.handler.file-based.nfs :as nfs-handler]
[frontend.handler.file-sync :as file-sync]
[frontend.handler.graph :as graph]
@@ -20,6 +21,7 @@
[frontend.util.fs :as fs-util]
[frontend.util.text :as text-util]
[goog.object :as gobj]
[lambdaisland.glogi :as log]
[logseq.shui.ui :as shui]
[medley.core :as medley]
[promesa.core :as p]
@@ -482,12 +484,11 @@
(state/set-state! :rtc/uploading? true)
(rtc-handler/<rtc-create-graph! repo)
(state/set-state! :rtc/uploading? false)
(rtc-handler/<rtc-start! repo))
(rtc-flows/trigger-rtc-start repo))
(p/catch (fn [error]
(reset! *creating-db? false)
(state/set-state! :rtc/uploading? false)
(prn :debug :create-db-failed)
(js/console.error error)))))
(log/error :create-db-failed error)))))
(reset! *creating-db? false)
(shui/dialog-close!))))))
submit! (fn [^js e click?]

View File

@@ -3,10 +3,12 @@
(:require [clojure.pprint :as pp]
[frontend.config :as config]
[frontend.db :as db]
[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.db :as ldb]
[logseq.db.common.sqlite :as sqlite-common-db]
[logseq.shui.ui :as shui]
@@ -91,7 +93,7 @@
(fn [e]
(util/stop e)
(p/do! (<rtc-branch-graph! repo)
(<rtc-start! repo)))}
(rtc-flows/trigger-rtc-start repo)))}
"Upload to server")]
:warning false))
@@ -117,8 +119,8 @@
(notification-download-higher-schema-graph! repo graph-uuid (:remote ex-data*))
:create-branch
(notification-upload-higher-schema-graph! repo)
;; else
(do (prn start-ex)
;; else
(do (log/info :start-ex start-ex)
(notification/show! [:div
[:div (:ex-message start-ex)]
[:div (-> ex-data*

View File

@@ -4,7 +4,6 @@
[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]
@@ -22,7 +21,7 @@
(run-background-task-when-not-publishing
;; try to restart rtc-loop when possible,
;; triggered by `rtc-flows/rtc-try-restart-flow`
::restart-rtc-task
::restart-rtc-to-reconnect
(m/reduce
(constantly nil)
(m/ap
@@ -51,21 +50,22 @@
"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)]
(let [logout-or-graph-switch (m/?> rtc-flows/logout-or-graph-switch-flow)]
(log/info :try-to-stop-rtc-if-needed logout-or-graph-switch)
(c.m/<? (rtc-handler/<rtc-stop!))))))
(run-background-task-when-not-publishing
;; auto-start rtc when [user-login graph-switch]
::auto-start-rtc-if-possible
(m/reduce
(constantly nil)
(m/ap
(let [start-reason (m/?> rtc-flows/trigger-start-rtc-flow)]
(log/info :try-to-start-rtc (first start-reason))
(c.m/<? (rtc-handler/<rtc-start! (state/get-current-repo)))))))

View File

@@ -73,3 +73,30 @@ conditions:
(true? js/navigator.onLine))
{:graph-uuid graph-uuid :t (common-util/time-ms)})))))
(c.m/throttle 5000)))
(def logout-or-graph-switch-flow
(c.m/mix
(m/eduction
(filter #(= :logout %))
flows/current-login-user-flow)
(m/eduction
(keep (fn [repo] (when repo :graph-switch)))
flows/current-repo-flow)))
(def ^:private *rtc-start-trigger (atom nil))
(defn trigger-rtc-start
[repo]
(assert (some? repo))
(reset! *rtc-start-trigger repo))
(def trigger-start-rtc-flow
(c.m/mix
(m/eduction
(keep (fn [user] (when (:email user) [:login user])))
flows/current-login-user-flow)
(m/eduction
(keep (fn [repo] (when repo [:graph-switch repo])))
flows/current-repo-flow)
(m/eduction
(keep (fn [repo] (when repo [:trigger-rtc repo])))
(m/watch *rtc-start-trigger))))

View File

@@ -14,8 +14,8 @@
[frontend.components.cmdk.core :as cmdk]
[frontend.components.diff :as diff]
[frontend.components.encryption :as encryption]
[frontend.components.file-sync :as file-sync]
[frontend.components.file-based.git :as git-component]
[frontend.components.file-sync :as file-sync]
[frontend.components.plugins :as plugin]
[frontend.components.property.dialog :as property-dialog]
[frontend.components.repo :as repo]
@@ -44,6 +44,7 @@
[frontend.handler.common.page :as page-common-handler]
[frontend.handler.db-based.property :as db-property-handler]
[frontend.handler.db-based.rtc :as rtc-handler]
[frontend.handler.db-based.rtc-flows :as rtc-flows]
[frontend.handler.editor :as editor-handler]
[frontend.handler.export :as export]
[frontend.handler.file-based.file :as file-handler]
@@ -175,8 +176,9 @@
(repo-config-handler/restore-repo-config! graph)
(when-not (= :draw (state/get-current-route))
(route-handler/redirect-to-home!))
(if db-based?
(rtc-handler/<rtc-start! graph)
(when-not db-based?
;; graph-switch will trigger a rtc-start automatically
;; (rtc-handler/<rtc-start! graph)
(file-sync-restart!))
(when-let [dir-name (and (not db-based?) (config/get-repo-dir graph))]
(fs/watch-dir! dir-name))
@@ -723,7 +725,7 @@
(defmethod handle :graph/restored [[_ graph]]
(when graph (assets-handler/ensure-assets-dir! graph))
(mobile/init!)
(rtc-handler/<rtc-start! graph)
(rtc-flows/trigger-rtc-start graph)
(fsrs/update-due-cards-count)
(when-not (mobile-util/native-ios?)
(state/pub-event! [:graph/ready graph])))