fix: don't save transit for db-based graphs

This commit is contained in:
Tienson Qin
2023-06-19 14:30:12 +08:00
parent 6ea438053f
commit 497e2dd3a9
3 changed files with 44 additions and 17 deletions

View File

@@ -208,7 +208,16 @@
(fs-extra/ensureDirSync dir)
dir))
(defn- get-graphs
(defn- get-db-based-graphs-dir
[]
(let [dir (if utils/ci?
(.resolve node-path js/__dirname "../tmp/graphs")
(.join node-path (.homedir os) "logseq" "graphs"))]
(fs-extra/ensureDirSync dir)
dir))
;; TODO: move file based graphs to "~/logseq/graphs" too
(defn- get-file-based-graphs
"Returns all graph names in the cache directory (starting with `logseq_local_`)"
[]
(let [dir (get-graphs-dir)]
@@ -217,6 +226,21 @@
(map #(node-path/basename % ".transit"))
(map graph-name->path))))
(defn- get-db-based-graphs
"Returns all graph names in the cache directory (starting with `logseq_db_`)"
[]
(let [dir (get-db-based-graphs-dir)]
(->> (common-graph/readdir dir)
(remove #{dir})
(map node-path/basename)
(map graph-name->path))))
(defn- get-graphs
[]
(concat
(get-file-based-graphs)
(get-db-based-graphs)))
;; TODO support alias mechanism
(defn get-graph-name
"Given a graph's name of string, returns the graph's fullname.

View File

@@ -7,15 +7,17 @@
[frontend.util :as util]
[promesa.core :as p]
[electron.ipc :as ipc]
[datascript.core :as d]))
[datascript.core :as d]
[frontend.config :as config]))
;; persisting DBs between page reloads
(defn persist! [repo]
(let [key (conn/datascript-db repo)
db (conn/get-db repo)]
(when db
(let [db-str (if db (db-utils/db->string db) "")]
(p/let [_ (db-persist/save-graph! key db-str)])))))
(when-not (config/db-based-graph? repo)
(let [key (conn/datascript-db repo)
db (conn/get-db repo)]
(when db
(let [db-str (if db (db-utils/db->string db) "")]
(p/let [_ (db-persist/save-graph! key db-str)]))))))
(defonce persistent-jobs (atom {}))
@@ -27,19 +29,20 @@
(defn persist-if-idle!
[repo]
(clear-repo-persistent-job! repo)
(let [job (js/setTimeout
(fn []
(if (and (state/input-idle? repo)
(state/db-idle? repo)
(when-not (config/db-based-graph? repo)
(let [job (js/setTimeout
(fn []
(if (and (state/input-idle? repo)
(state/db-idle? repo)
;; It's ok to not persist here since new changes
;; will be notified when restarting the app.
(not (state/whiteboard-route?)))
(persist! repo)
(not (state/whiteboard-route?)))
(persist! repo)
;; (state/set-db-persisted! repo true)
(persist-if-idle! repo)))
3000)]
(swap! persistent-jobs assoc repo job)))
(persist-if-idle! repo)))
3000)]
(swap! persistent-jobs assoc repo job))))
;; only save when user's idle

View File

@@ -545,7 +545,7 @@
(p/let [full-graph-name (str config/db-version-prefix graph)
_ (start-repo-db-if-not-exists! full-graph-name)
_ (state/add-repo! {:url full-graph-name})
_ (ipc/ipc :db-new graph)
_ (ipc/ipc :db-new full-graph-name)
_ (db/transact! full-graph-name [(react/kv :db/type "db")]
{:skip-persist? true})
initial-data [{:file/path (str "logseq/" "config.edn")