Procházet zdrojové kódy

refactor(fs): handle global config as state, not in db

Andelf před 2 roky
rodič
revize
a051ded28e

+ 4 - 4
src/main/frontend/fs/nfs.cljs

@@ -1,10 +1,10 @@
-(ns ^:no-doc frontend.fs.nfs
+(ns frontend.fs.nfs
   "Browser File System API based fs implementation.
    
    Rationale:
-   nfs-file-handles-cache stores all file & directory handle
-   idb stores top-level directory handle
-   readdir/get-files is called by re-index and initial watcher to init all handles"
+   - nfs-file-handles-cache stores all file & directory handle
+   - idb stores top-level directory handle
+   - readdir/get-files is called by re-index and initial watcher to init all handles"
   (:require [frontend.fs.protocol :as protocol]
             [frontend.util :as util]
             [clojure.string :as string]

+ 8 - 2
src/main/frontend/fs/watcher_handler.cljs

@@ -17,7 +17,8 @@
             [lambdaisland.glogi :as log]
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.util.block-ref :as block-ref]
-            [promesa.core :as p]))
+            [promesa.core :as p]
+            [frontend.handler.global-config :as global-config-handler]))
 
 ;; all IPC paths must be normalized! (via gp-util/path-normalize)
 
@@ -75,6 +76,12 @@
           (let [backup? (not (string/blank? db-content))]
             (handle-add-and-change! repo path content db-content mtime backup?))
 
+          ;; global config handling
+          (and (= "change" type)
+               (= dir (global-config-handler/global-config-dir)))
+          (when (= path "config.edn")
+            (global-config-handler/set-global-config-state! content))
+
           (and (= "change" type)
                (not (db/file-exists? repo path)))
           (js/console.error "Can't get file in the db: " path)
@@ -89,7 +96,6 @@
                          (string/trim (or (state/get-default-journal-template) "")))
                       (= (string/trim content) "-")
                       (= (string/trim content) "*")))
-            (prn ::fuck!!)
             (handle-add-and-change! repo path content db-content mtime (not global-dir))) ;; no backup for global dir
 
           (and (= "unlink" type)

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

@@ -44,7 +44,7 @@
                   rpath (path/trim-dir-prefix repo-dir path)]
               (if rpath
                 ;; in-db file
-                (let [old-content (db/get-file rpath)]
+                (let [old-content (or (db/get-file rpath) "")]
                   (when (and
                          (not (string/blank? value))
                          (not= (string/trim value) (string/trim old-content)))

+ 2 - 5
src/main/frontend/handler/common/file.cljs

@@ -46,13 +46,11 @@
   (graph-parser/get-blocks-to-delete db file-page file-path retain-uuid-blocks))
 
 (defn reset-file!
-  "Main fn for updating a db with the results of a parsed file.
- "
+  "Main fn for updating a db with the results of a parsed file"
   ([repo-url file-path content]
    (reset-file! repo-url file-path content {}))
   ([repo-url file-path content {:keys [verbose] :as options}]
-   (let [_ (prn ::reset-file file-path)
-         new? (nil? (db/entity [:file/path file-path]))
+   (let [new? (nil? (db/entity [:file/path file-path]))
          options (merge (dissoc options :verbose)
                         {:new? new?
                          :delete-blocks-fn (partial validate-and-get-blocks-to-delete repo-url)
@@ -61,7 +59,6 @@
                                             :date-formatter (state/get-date-formatter)
                                             :block-pattern (config/get-block-pattern (gp-util/get-format file-path))
                                             :supported-formats (gp-config/supported-formats)
-                                           ;; :uri-encoded? (boolean (mobile-util/native-platform?))
                                             :filename-format (state/get-filename-format repo-url)
                                             :extracted-block-ids (:extracted-block-ids options)}
                                            (when (some? verbose) {:verbose verbose}))})]

+ 0 - 1
src/main/frontend/handler/config.cljs

@@ -3,7 +3,6 @@
   (:require [frontend.state :as state]
             [frontend.handler.file :as file-handler]
             [frontend.handler.repo-config :as repo-config-handler]
-            [frontend.config :as config]
             [frontend.db :as db]
             [borkdude.rewrite-edn :as rewrite]))
 

+ 1 - 7
src/main/frontend/handler/file.cljs

@@ -116,13 +116,6 @@
   [repo path content write-file-options]
   (let [original-content (db/get-file repo path)
         path-dir (config/get-repo-dir repo)
-        ;;path-dir (if (and
-        ;;              (config/global-config-enabled?)
-         ;;             ;; Hack until we better understand failure in error handler
-          ;;            (global-config-handler/global-config-dir-exists?)
-          ;;            (= (path/dirname path) (global-config-handler/global-config-dir)))
-           ;;        (global-config-handler/global-config-dir)
-          ;;         (config/get-repo-dir repo))
         write-file-options' (merge write-file-options
                                    (when original-content {:old-content original-content}))]
     (fs/write-file! repo path-dir path content write-file-options')))
@@ -138,6 +131,7 @@
         (state/pub-event! [:shortcut/refresh])))))
 
 (defn alter-file
+  "Write any in-DB file, e.g. repo config, page, whiteboard, etc."
   [repo path content {:keys [reset? re-render-root? from-disk? skip-compare? new-graph? verbose
                              skip-db-transact? extracted-block-ids]
                       :or {reset? true

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

@@ -29,7 +29,7 @@
   []
   (path/path-join @root-dir "config" "config.edn"))
 
-(defn- set-global-config-state!
+(defn set-global-config-state!
   [content]
   (let [config (edn/read-string content)]
     (state/set-global-config! config)

+ 24 - 60
src/main/frontend/handler/web/nfs.cljs

@@ -135,58 +135,43 @@
               _ (when (fn? on-open-dir)
                   (prn ::calling-on-open-dir-fn)
                   (on-open-dir result))
-              root-handle (:path result)
-              _ (when (fn? picked-root-fn) (picked-root-fn root-handle))
-              dir-name root-handle
-              repo (str config/local-db-prefix root-handle)]
-
+              root-dir (:path result)
+              ;; calling when root picked
+              _ (when (fn? picked-root-fn) (picked-root-fn root-dir))
+              repo (str config/local-db-prefix root-dir)]
         (state/set-loading-files! repo true)
         (when-not (or (state/home?) (state/setups-picker?))
           (route-handler/redirect-to-home! false))
         (reset! *repo repo)
-        (prn ::begin-hanlding-files dir-name)
-        (when-not (string/blank? dir-name)
-          (p/let [; handle/logseq_local_dir-name
-                  ;;_ (when-let [root-handle-path (and nfs?
-                    ;;                                 (str config/local-handle-prefix dir-name))]
-                      ;;(prn ::saving-handle-to-idb)
-                      ; (idb/set-item! root-handle-path (str "handle/" root-handle)))
-                      ; (nfs/save-root-handle-to-idb! repo root-handle))
-                 ;      (idb/set-item! root-handle-path (str "handle/" root-handle))
-                 ;     ; (nfs/add-nfs-file-handle! root-handle-path root-handle)
-                 ;     )
-                  files (:files result)
+        (prn ::begin-hanlding-files root-dir)
+        (when-not (string/blank? root-dir)
+          (p/let [files (:files result)
                   files (-> (->db-files files nfs?)
                             ;; NOTE: filter, in case backend does not handle this
-                            (remove-ignore-files dir-name nfs?))
+                            (remove-ignore-files root-dir nfs?))
                   _ (prn ::remain-files files)
                   markup-files (filter-markup-and-built-in-files files)]
             (-> files
                 (p/then (fn [result]
                           ;; handle graphs txid
                           (p/let [files (mapv #(dissoc % :file/file) result)
-                                  graphs-txid-meta (util-fs/read-graphs-txid-info dir-name)
+                                  graphs-txid-meta (util-fs/read-graphs-txid-info root-dir)
                                   graph-uuid (and (vector? graphs-txid-meta) (second graphs-txid-meta))]
                             (if-let [exists-graph (state/get-sync-graph-by-id graph-uuid)]
                               (state/pub-event!
                                [:notification/show
                                 {:content (str "This graph already exists in \"" (:root exists-graph) "\"")
                                  :status :warning}])
-                              (do
-                                (prn ::prepare-load-new-repo files)
-                                (repo-handler/start-repo-db-if-not-exists! repo)
-                                (prn ::dd (nil? (seq markup-files)))
-                                (p/do!
-                                 (repo-handler/load-new-repo-to-db! repo
-                                                                    {:new-graph?   true
-                                                                     :empty-graph? (nil? (seq markup-files))
-                                                                     :file-objs    files})
-                                 (prn ::debug-2.5)
-                                 (state/add-repo! {:url repo :nfs? true})
-                                 (prn ::debug-33333)
-                                 (state/set-loading-files! repo false)
-                                 (when ok-handler (ok-handler {:url repo}))
-                                 (db/persist-if-idle! repo)))))))
+                              (p/do! (repo-handler/start-repo-db-if-not-exists! repo)
+                                     (global-config-handler/restore-global-config!)
+                                     (repo-handler/load-new-repo-to-db! repo
+                                                                        {:new-graph?   true
+                                                                         :empty-graph? (nil? (seq markup-files))
+                                                                         :file-objs    files})
+                                     (state/add-repo! {:url repo :nfs? true})
+                                     (state/set-loading-files! repo false)
+                                     (when ok-handler (ok-handler {:url repo}))
+                                     (db/persist-if-idle! repo))))))
                 (p/catch (fn [error]
                            (log/error :nfs/load-files-error repo)
                            (log/error :exception error)))))))
@@ -241,7 +226,7 @@
      :deleted  deleted}))
 
 (defn- handle-diffs!
-  "Compute directory diffs and handle them."
+  "Compute directory diffs and (re)load repo"
   [repo nfs? old-files new-files re-index? ok-handler]
   (let [get-last-modified-at (fn [path] (some (fn [file]
                                                 (when (= path (:file/path file))
@@ -270,7 +255,6 @@
                                (rename-f "modify" modified))]
                     (when (or (and (seq diffs) (seq modified-files))
                               (seq diffs))
-                      (comment "re-index a local graph is handled here")
                       (-> (repo-handler/load-repo-to-db! repo
                                                          {:diffs     diffs
                                                           :nfs-files modified-files
@@ -289,7 +273,6 @@
   "Handle refresh and re-index"
   [repo {:keys [re-index? ok-handler]
          :or {re-index? false}}]
-  (prn ::reload-dir)
   (when (and repo (config/local-db? repo))
     (let [old-files (db/get-files-full repo)
           repo-dir (config/get-local-dir repo)
@@ -302,33 +285,14 @@
         (state/set-graph-syncing? true))
       (->
        (p/let [handle (when-not electron? (idb/get-item handle-path))]
-         (prn ::handle handle)
          (when (or handle electron? mobile-native?)
            (p/let [_ (when nfs? (nfs/verify-permission repo true))
                    local-files-result (fs/get-files repo-dir)
-                   _ (prn ::reading-local-fils local-files-result)
+                   _ (when (config/global-config-enabled?)
+                       ;; reload global config into state
+                       (global-config-handler/restore-global-config!))
                    new-files (-> (->db-files (:files local-files-result) nfs?)
-                                       (remove-ignore-files repo-dir nfs?))
-                   _ (prn ::new-local-files new-files)
-                  ;; new-global-files (if (and (config/global-config-enabled?)
-                   ;;                          ;; Hack until we better understand failure in frontend.handler.file/alter-file
-                    ;;                         (global-config-handler/global-config-dir-exists?))
-                     ;;                 (p/let [global-files-result (fs/get-files
-                      ;;                                             (global-config-handler/global-config-dir)
-                       ;;                                            (constantly nil))
-                        ;;                      global-files (-> (->db-files (global-config-handler/global-config-dir) global-files-result)
-                         ;;                                      (remove-ignore-files (global-config-handler/global-config-dir) nfs?))]
-                          ;;              global-files)
-                           ;;           (p/resolved [])) 
-                   _ (comment when nfs?
-                              (let [file-paths (set (map :file/path new-files))]
-                                (swap! path-handles (fn [handles]
-                                                      (->> handles
-                                                           (filter (fn [[path _handle]]
-                                                                     (contains? file-paths
-                                                                                (string/replace-first path (str dir-name "/") ""))))
-                                                           (into {})))))
-                              (set-files! @path-handles))]
+                                 (remove-ignore-files repo-dir nfs?))]
              (handle-diffs! repo nfs? old-files new-files re-index? ok-handler))))
        (p/catch (fn [error]
                   (log/error :nfs/load-files-error repo)