Browse Source

electron: add re-index support

Tienson Qin 5 years ago
parent
commit
a73b4e242f

+ 13 - 6
src/electron/electron/handler.cljs

@@ -41,12 +41,9 @@
 (defmethod handle :stat [_window [_ path]]
   (fs/statSync path))
 
-;; TODO: Is it going to be slow if it's a huge directory
-(defmethod handle :openDir [window _messages]
-  (let [result (.showOpenDialogSync dialog (bean/->js
-                                            {:properties ["openDirectory"]}))
-        path (first result)
-        result (->> (map
+(defn- get-files
+  [path]
+  (let [result (->> (map
                       (fn [path]
                         (let [stat (fs/statSync path)]
                           (when-not (.isDirectory stat)
@@ -57,6 +54,16 @@
                     (remove nil?))]
     (vec (cons {:path path} result))))
 
+;; TODO: Is it going to be slow if it's a huge directory
+(defmethod handle :openDir [window _messages]
+  (let [result (.showOpenDialogSync dialog (bean/->js
+                                            {:properties ["openDirectory"]}))
+        path (first result)]
+    (get-files path)))
+
+(defmethod handle :getFiles [window [_ path]]
+  (get-files path))
+
 (defmethod handle :default [args]
   (println "Error: no ipc handler for: " (bean/->js args)))
 

+ 9 - 0
src/main/frontend/fs.cljs

@@ -96,6 +96,15 @@
           [(:path dir) paths])
         result))))
 
+(defn get-files
+  [path-or-handle ok-handler]
+  (let [record (if (util/electron?) node-record nfs-record)]
+    (p/let [result (protocol/get-files record path-or-handle ok-handler)]
+      (if (util/electron?)
+        (let [[dir & paths] (bean/->clj result)]
+          [(:path dir) paths])
+        result))))
+
 (defn mkdir-if-not-exists
   [dir]
   (when dir

+ 2 - 0
src/main/frontend/fs/bfs.cljs

@@ -33,4 +33,6 @@
   (stat [this dir path]
     (js/window.pfs.stat (str dir path)))
   (open-dir [this ok-handler]
+    nil)
+  (get-files [this path-or-handle ok-handler]
     nil))

+ 3 - 1
src/main/frontend/fs/nfs.cljs

@@ -196,4 +196,6 @@
       (p/rejected "File not exists")))
   (open-dir [this ok-handler]
     (utils/openDirectory #js {:recursive true}
-                         ok-handler)))
+                         ok-handler))
+  (get-files [this path-or-handle ok-handler]
+    (utils/getFiles path-or-handle true ok-handler)))

+ 3 - 1
src/main/frontend/fs/node.cljs

@@ -42,4 +42,6 @@
     (let [path (concat-path dir path)]
       (ipc/ipc "stat" path)))
   (open-dir [this ok-handler]
-    (ipc/ipc "openDir" {})))
+    (ipc/ipc "openDir" {}))
+  (get-files [this path-or-handle ok-handler]
+    (ipc/ipc "getFiles" path-or-handle)))

+ 2 - 1
src/main/frontend/fs/protocol.cljs

@@ -9,4 +9,5 @@
   (write-file! [this repo dir path content opts])
   (rename! [this repo old-path new-path])
   (stat [this dir path])
-  (open-dir [this ok-handler]))
+  (open-dir [this ok-handler])
+  (get-files [this path-or-handle ok-handler]))

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

@@ -98,6 +98,7 @@
       (nfs/add-nfs-file-handle! path handle))
     (set-files-aux! handles)))
 
+;; TODO: extract code for `ls-dir-files` and `reload-dir!`
 (defn ls-dir-files
   []
   (let [path-handles (atom {})
@@ -213,30 +214,34 @@
            dir-name (config/get-local-dir repo)
            handle-path (str config/local-handle-prefix dir-name)
            path-handles (atom {})
-           electron? (util/electron?)]
+           electron? (util/electron?)
+           nfs? (not electron?)]
        (state/set-graph-syncing? true)
        (->
         (p/let [handle (idb/get-item handle-path)]
-          (when handle
+          (when (or handle electron?)   ; electron doesn't store the file handle
             (p/let [_ (when handle (nfs/verify-permission repo handle true))
-                    files-result (utils/getFiles handle true
-                                                 (fn [path handle]
-                                                   (swap! path-handles assoc path handle)))
-                    new-files (-> (->db-files electron? dir-name files-result)
+                    files-result (fs/get-files (if nfs? handle
+                                                   (config/get-local-dir repo))
+                                               (fn [path handle]
+                                                 (when nfs?
+                                                   (swap! path-handles assoc path handle))))
+                    new-files (-> (->db-files electron? dir-name (second files-result))
                                   remove-ignore-files)
-                    _ (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)
+                    _ (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))
                     get-file-f (fn [path files] (some #(when (= (:file/path %) path) %) files))
                     {:keys [added modified deleted] :as diffs} (compute-diffs old-files new-files)
                     ;; Use the same labels as isomorphic-git
                     rename-f (fn [typ col] (mapv (fn [file] {:type typ :path file}) col))
-                    _ (when (seq deleted)
+                    _ (when (and nfs? (seq deleted))
                         (let [deleted (doall
                                        (-> (map (fn [path] (if (= "/" (first path))
                                                              path
@@ -247,13 +252,15 @@
                                           (idb/remove-item! handle-path)
                                           (nfs/remove-nfs-file-handle! handle-path))) deleted))))
                     added-or-modified (set (concat added modified))
-                    _ (when (seq added-or-modified)
+                    _ (when (and nfs? (seq added-or-modified))
                         (p/all (map (fn [path]
                                       (when-let [handle (get @path-handles path)]
                                         (idb/set-item! (str handle-path path) handle))) added-or-modified)))]
               (-> (p/all (map (fn [path]
                                 (when-let [file (get-file-f path new-files)]
-                                  (p/let [content (.text (:file/file file))]
+                                  (p/let [content (if nfs?
+                                                    (.text (:file/file file))
+                                                    (:file/content file))]
                                     (assoc file :file/content content)))) added-or-modified))
                   (p/then (fn [result]
                             (let [files (map #(dissoc % :file/file :file/handle) result)