fix: don't start rtc for local db graphs

Also, stop rtc when switching into a local graph.
pull/11055/head
Tienson Qin 2024-03-17 13:47:19 +08:00
parent cd1c71291b
commit c4e6db9afb
8 changed files with 64 additions and 38 deletions

View File

@ -585,6 +585,10 @@
:block/content ""
:block/format :markdown})
(defn get-graph-rtc-uuid
[db]
(when db (:graph/uuid (d/entity db :graph/uuid))))
(comment
(defn db-based-graph?
"Whether the current graph is db-only"

View File

@ -154,12 +154,15 @@
(defn get-initial-data
"Returns initial data"
[db]
(let [favorites (get-favorites db)
(let [idents (remove nil?
(when-let [id (:graph/uuid (d/entity db :graph/uuid))]
[{:db/ident :graph/uuid :graph/uuid id}]))
favorites (get-favorites db)
latest-journals (get-latest-journals db 3)
all-files (get-all-files db)
home-page-data (get-home-page db all-files)
structured-blocks (get-structured-blocks db)]
(concat favorites latest-journals all-files home-page-data structured-blocks)))
(concat idents favorites latest-journals all-files home-page-data structured-blocks)))
(defn restore-initial-data
"Given initial sqlite data and schema, returns a datascript connection"

View File

@ -23,7 +23,9 @@
[frontend.version :refer [version]]
[reitit.frontend.easy :as rfe]
[rum.core :as rum]
[clojure.string :as string]))
[clojure.string :as string]
[frontend.db :as db]
[logseq.db :as ldb]))
(rum/defc home-button
< {:key-fn #(identity "home-button")}
@ -238,7 +240,8 @@
[:div.r.flex.drag-region
(when (and current-repo
config/dev? (user-handler/logged-in?)
(config/db-based-graph? current-repo))
(config/db-based-graph? current-repo)
(some? (ldb/get-graph-rtc-uuid (db/get-db current-repo))))
(rtc-indicator/indicator))
(when (and current-repo

View File

@ -4,7 +4,9 @@
[cljs-bean.core :as bean]
[promesa.core :as p]
[frontend.config :as config]
[frontend.handler.user :as user-handler]))
[frontend.handler.user :as user-handler]
[frontend.db :as db]
[logseq.db :as ldb]))
(defn <rtc-create-graph!
[repo]
@ -30,11 +32,17 @@
(defn <rtc-start!
[repo]
(when-let [^js worker @state/*db-worker]
(user-handler/<wrap-ensure-id&access-token
(let [token (state/get-auth-id-token)]
(.rtc-start worker repo token
(and config/dev?
(state/sub [:ui/developer-mode?])))))))
(when (ldb/get-graph-rtc-uuid (db/get-db repo))
(user-handler/<wrap-ensure-id&access-token
(let [token (state/get-auth-id-token)]
(.rtc-start worker repo token
(and config/dev?
(state/sub [:ui/developer-mode?]))))))))
(defn <rtc-stop!
[]
(when-let [^js worker @state/*db-worker]
(.rtc-stop worker)))
;; TODO: shared graphs need `shared-by`, user name
(defn <get-remote-graphs

View File

@ -177,7 +177,9 @@
(srs/update-cards-due-count!)
(state/pub-event! [:graph/ready graph])
(if db-based?
(rtc-handler/<rtc-start! graph)
(p/do!
(rtc-handler/<rtc-stop!)
(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)))))))

View File

@ -1082,20 +1082,23 @@
([repo]
(get-debug-state repo @*state))
([repo state]
(let [graph-uuid (op-mem-layer/get-graph-uuid repo)
local-tx (op-mem-layer/get-local-tx repo)
unpushed-block-update-count (op-mem-layer/get-unpushed-block-update-count repo)]
(cond->
{:graph-uuid graph-uuid
:local-tx local-tx
:unpushed-block-update-count unpushed-block-update-count}
state
(merge
{:user-uuid (:user-uuid state)
:rtc-state @(:*rtc-state state)
:ws-state (some-> @(:*ws state) ws/get-state)
:auto-push-updates? (when-let [a (:*auto-push-client-ops? state)]
@a)})))))
(let [*conn (:*db-conn state)
conn (when *conn @*conn)]
(when conn
(let [graph-uuid (ldb/get-graph-rtc-uuid @conn)
local-tx (op-mem-layer/get-local-tx repo)
unpushed-block-update-count (op-mem-layer/get-unpushed-block-update-count repo)]
(cond->
{:graph-uuid graph-uuid
:local-tx local-tx
:unpushed-block-update-count unpushed-block-update-count}
state
(merge
{:user-uuid (:user-uuid state)
:rtc-state @(:*rtc-state state)
:ws-state (some-> @(:*ws state) ws/get-state)
:auto-push-updates? (when-let [a (:*auto-push-client-ops? state)]
@a)})))))))
(defn get-block-update-log
([block-uuid]
@ -1120,12 +1123,16 @@
(when reset-*state?
(reset! *state state)
(swap! *state update :counter inc))
(add-watch (:*rtc-state @*state) :update-rtc-state
(fn [_ _ _ _new]
(when (:*repo @*state)
(swap! *state update :counter (fnil inc 0)))))
state))))
(defn <start-rtc
[repo conn token dev-mode?]
(go
(if-let [graph-uuid (op-mem-layer/get-graph-uuid repo)]
(if-let [graph-uuid (ldb/get-graph-rtc-uuid @conn)]
(let [state (<! (<init-state repo token true {:dev-mode? dev-mode?}))
state-for-asset-sync (asset-sync/init-state-from-rtc-state state)
_ (reset! asset-sync/*asset-sync-state state-for-asset-sync)
@ -1186,7 +1193,7 @@
(= :open (:rtc-state new-state)))
(worker-util/post-message :rtc-sync-state new-state))))))
(add-watch op-mem-layer/*ops-store :update-rtc-state
(add-watch op-mem-layer/*ops-store :update-ops-state
(fn [_ _ _ _new]
(when (:*repo @*state)
(swap! *state update :counter (fnil inc 0)))))

View File

@ -128,16 +128,18 @@
blocks)))
(defn- <transact-remote-all-blocks-to-sqlite
[all-blocks repo]
[all-blocks repo graph-uuid]
(go-try
(let [{:keys [t blocks]} all-blocks
blocks* (replace-db-id-with-temp-id blocks)
blocks-with-page-id (fill-block-fields blocks*)
tx-data (concat blocks-with-page-id
[{:db/ident :graph/uuid :graph/uuid graph-uuid}])
^js worker-obj (:worker/object @worker-state/*state)
work (p/do!
(.createOrOpenDB worker-obj repo {:close-other-db? false})
(.exportDB worker-obj repo)
(.transact worker-obj repo blocks-with-page-id {:rtc-download-graph? true} (worker-state/get-context))
(.transact worker-obj repo tx-data {:rtc-download-graph? true} (worker-state/get-context))
(.closeDB worker-obj repo))]
(<? (p->c work))
@ -154,11 +156,15 @@
repo (str "logseq_db_" repo)]
(if (not= 200 status)
(ex-info "<download-graph failed" r)
(let [all-blocks (transit/read transit-r body)]
(let [^js worker-obj (:worker/object @worker-state/*state)
all-blocks (transit/read transit-r body)]
(worker-state/set-rtc-downloading-graph! true)
(op-mem-layer/init-empty-ops-store! repo)
(<? (<transact-remote-all-blocks-to-sqlite all-blocks repo))
(<? (<transact-remote-all-blocks-to-sqlite all-blocks repo graph-uuid))
(op-mem-layer/update-graph-uuid! repo graph-uuid)
(prn ::download-graph repo (@@#'op-mem-layer/*ops-store repo))
(<! (op-mem-layer/<sync-to-idb-layer! repo))
(<! (p->c
(p/do!
(.storeMetadata worker-obj repo (pr-str {:graph/uuid (:graph-uuid r)})))))
(worker-state/set-rtc-downloading-graph! false))))))

View File

@ -434,13 +434,6 @@
(assert (contains? repo-ops-store :current-branch) repo)
(asset-uuid->ops asset-uuid))))
(defn get-graph-uuid
[repo]
(some-> (get @*ops-store repo)
:current-branch
:graph-uuid))
(defn get-local-tx
[repo]
(some-> (get @*ops-store repo)