Prechádzať zdrojové kódy

enhance: save sqlite.db when closing any Electron window

Tienson Qin 1 rok pred
rodič
commit
f2c7cad4a5

+ 1 - 0
src/electron/electron/backup_file.cljs

@@ -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]

+ 1 - 5
src/electron/electron/window.cljs

@@ -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)))))
 

+ 18 - 7
src/main/electron/listener.cljs

@@ -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

+ 1 - 1
src/main/frontend/handler/repo.cljs

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