enhance: save sqlite.db when closing any Electron window

This commit is contained in:
Tienson Qin
2023-12-11 18:34:36 +08:00
parent f25b7e06f9
commit f2c7cad4a5
4 changed files with 21 additions and 13 deletions

View File

@@ -24,6 +24,7 @@
[repo relative-path]
(get-backup-dir* repo relative-path version-file-dir))
;; TODO: add interval support like days
(defn- truncate-old-versioned-files!
"reserve the latest 6 version files"
[dir]

View File

@@ -95,12 +95,8 @@
(.send web-contents "persist-zoom-level" (.getZoomLevel web-contents))
(.send web-contents "persistent-dbs"))
(async/go
(let [_ (async/<! state/persistent-dbs-chan)]
(let [result (async/<! state/persistent-dbs-chan)]
(destroy-window! win)
;; (if @*quitting?
;; (doseq [win (get-all-windows)]
;; (destroy-window! win))
;; (destroy-window! win))
(when @*quitting?
(async/put! state/persistent-dbs-chan true)))))

View File

@@ -16,11 +16,13 @@
[frontend.handler.user :as user]
[frontend.handler.search :as search-handler]
[frontend.state :as state]
[frontend.config :as config]
[frontend.ui :as ui]
[logseq.common.path :as path]
[logseq.graph-parser.util :as gp-util]
[promesa.core :as p]
[frontend.handler.property.util :as pu]))
[frontend.handler.property.util :as pu]
[frontend.persist-db :as persistent-db]))
(defn- safe-api-call
"Force the callback result to be nil, otherwise, ipc calls could lead to
@@ -30,18 +32,27 @@
(defn persist-dbs!
[]
;; only persist current db!
;; TODO rename the function and event to persist-db
(repo-handler/persist-db! {:before #(ui/notify-graph-persist!)
:on-success #(ipc/ipc "persistent-dbs-saved")
:on-error #(ipc/ipc "persistent-dbs-error")}))
(when-let [repo (state/get-current-repo)]
(let [error-handler (fn [error]
(prn :debug :persist-db-failed :repo repo)
(js/console.error error)
(notification/show! error :error))]
(if (config/db-based-graph? repo)
(->
(p/do!
(persistent-db/<export-db repo)
(ipc/ipc "persistent-dbs-saved"))
(p/catch error-handler))
;; TODO: Move all file based graphs to use the above persist approach
(repo-handler/persist-db! {:before ui/notify-graph-persist!
:on-success #(ipc/ipc "persistent-dbs-saved")
:on-error error-handler})))))
(defn listen-persistent-dbs!
[]
(safe-api-call "persistent-dbs" (fn [_data] (persist-dbs!))))
(defn ^:large-vars/cleanup-todo listen-to-electron!
[]
;; TODO: move "file-watcher" to electron.ipc.channels

View File

@@ -462,7 +462,7 @@
{:error error
:payload {:type :db/persist-failed}}])
(when on-error
(on-error)))))))
(on-error error)))))))
(defn get-repos
[]