Browse Source

feat(chrome native fs): support refresh to sync

Tienson Qin 5 years ago
parent
commit
58228dc336

+ 10 - 8
src/main/frontend/components/block.cljs

@@ -1485,9 +1485,9 @@
 
 (rum/defcs custom-query < rum/reactive
   {:will-mount (fn [state]
-                 (let [[config query] (:rum/args state)]
-                   (let [query-atom (db/custom-query query)]
-                     (assoc state :query-atom query-atom))))
+                 (let [[config query] (:rum/args state)
+                       query-atom (db/custom-query query)]
+                   (assoc state :query-atom query-atom)))
    :did-mount (fn [state]
                 (when-let [query (last (:rum/args state))]
                   (state/add-custom-query-component! query (:rum/react-component state)))
@@ -1535,11 +1535,13 @@
                                :margin-left "0.25rem"}})
 
             (seq result)                     ;TODO: table
-            [:pre
-             (for [record result]
-               (if (map? record)
-                 (str (util/pp-str record) "\n")
-                 record))]
+            (let [result (->>
+                          (for [record result]
+                            (if (map? record)
+                              (str (util/pp-str record) "\n")
+                              record))
+                          (remove nil?))]
+              [:pre result])
 
             :else
             [:div.text-sm.mt-2.ml-2.font-medium.opacity-50 "Empty"])

+ 2 - 2
src/main/frontend/db.cljs

@@ -926,12 +926,12 @@
       @conn)
      (into {}))))
 
-(defn get-files-path-size-modified-at
+(defn get-files-full
   [repo]
   (when-let [conn (get-files-conn repo)]
     (->>
      (d/q
-      '[:find (pull ?file [:file/path :file/size :file/last-modified-at])
+      '[:find (pull ?file [*])
         :where
         [?file :file/path]]
       @conn)

+ 16 - 11
src/main/frontend/handler/repo.cljs

@@ -157,7 +157,7 @@
   (create-custom-theme repo-url))
 
 (defn- parse-files-and-load-to-db!
-  [repo-url files {:keys [first-clone? delete-files delete-blocks re-render?]}]
+  [repo-url files {:keys [first-clone? delete-files delete-blocks re-render? re-render-opts]}]
   (state/set-state! :repo/loading-files? false)
   (state/set-state! :repo/importing-to-db? true)
   (let [file-paths (map :file/path files)
@@ -176,9 +176,9 @@
                                     (:file/content %)) files)]
           (file-handler/restore-config! repo-url content true))))
     (when first-clone? (create-default-files! repo-url))
-    (state/set-state! :repo/importing-to-db? false)
     (when re-render?
-      (ui-handler/re-render-root!))))
+      (ui-handler/re-render-root! re-render-opts))
+    (state/set-state! :repo/importing-to-db? false)))
 
 (defn load-repo-to-db!
   [repo-url {:keys [first-clone? diffs nfs-files]}]
@@ -187,9 +187,9 @@
                         (file-handler/load-files-contents!
                          repo-url
                          files
-                         (fn [contents] (parse-files-and-load-to-db! repo-url files option))))]
+                         (fn [files-contents] (parse-files-and-load-to-db! repo-url files-contents option))))]
     (cond
-      (seq nfs-files)
+      (and (not (seq diffs)) (seq nfs-files))
       (parse-files-and-load-to-db! repo-url nfs-files {:first-clone? true})
 
       first-clone?
@@ -216,12 +216,17 @@
               delete-pages (if (seq remove-files)
                              (db/delete-pages-by-files remove-files)
                              [])
-              add-or-modify-files (util/remove-nils (concat add-files modify-files))]
-          (load-contents add-or-modify-files
-                         {:first-clone? first-clone?
-                          :delete-files (concat delete-files delete-pages)
-                          :delete-blocks delete-blocks
-                          :re-render? true}))))))
+              add-or-modify-files (some->>
+                                   (concat modify-files add-files)
+                                   (util/remove-nils))
+              options {:first-clone? first-clone?
+                       :delete-files (concat delete-files delete-pages)
+                       :delete-blocks delete-blocks
+                       :re-render? true}]
+          (if (seq nfs-files)
+            (parse-files-and-load-to-db! repo-url nfs-files
+                                         (assoc options :re-render-opts {:clear-all-query-state? true}))
+            (load-contents add-or-modify-files options)))))))
 
 (defn persist-repo!
   [repo]

+ 12 - 7
src/main/frontend/handler/ui.cljs

@@ -47,12 +47,17 @@
 
 
 (defn re-render-root!
-  []
-  (when-let [component (state/get-root-component)]
-    (db/clear-query-state-without-refs-and-embeds!)
-    (rum/request-render component)
-    (doseq [component (state/get-custom-query-components)]
-      (rum/request-render component))))
+  ([]
+   (re-render-root! {}))
+  ([{:keys [clear-all-query-state?]
+     :or {clear-all-query-state? false}}]
+   (when-let [component (state/get-root-component)]
+     (if clear-all-query-state?
+       (db/clear-query-state!)
+       (db/clear-query-state-without-refs-and-embeds!))
+     (rum/request-render component)
+     (doseq [component (state/get-custom-query-components)]
+       (rum/request-render component)))))
 
 (defn re-render-file!
   []
@@ -92,5 +97,5 @@
                     (state/get-custom-css-link)
                     (db/get-custom-css)
                     ;; (state/get-custom-css-link)
-                    )]
+)]
     (util/add-style! style)))

+ 56 - 36
src/main/frontend/handler/web/nfs.cljs

@@ -14,7 +14,8 @@
             [frontend.ui :as ui]
             [frontend.fs :as fs]
             [frontend.db :as db]
-            [frontend.config :as config]))
+            [frontend.config :as config]
+            [lambdaisland.glogi :as log]))
 
 (defn- ->db-files
   [dir-name result]
@@ -74,11 +75,9 @@
 
                        (state/add-repo! {:url repo :nfs? true}))))
            (p/catch (fn [error]
-                      (println "Load files content error: ")
-                      (js/console.dir error)))))
+                      (log/error :nfs/load-files-error error)))))
      (p/catch (fn [error]
-                (println "Open directory error: ")
-                (js/console.dir error))))))
+                (log/error :nfs/open-dir-error error))))))
 
 (defn open-file-picker
   "Shows a file picker that lets a user select a single existing file, returning a handle for the selected file. "
@@ -125,31 +124,30 @@
                        set)))
         old-files (->set old-files ks)
         new-files (->set new-files ks)
-        diff (fn [col1 col2]
-               (->> (set/difference col1 col2)
-                    (map :file/path)
-                    set))
-        new-diff (diff new-files old-files)
-        old-diff (diff old-files new-files)]
-    {:added (set/difference new-diff old-diff)
-     :modified (let [both-exist (set/union new-diff old-diff)]
-                 (when (seq both-exist)
-                   (->>
-                    (filter (fn [{:file/keys [path last-modified-at]}]
-                              (when-let [old-file (some #(when (= (:file/path %) path) %) old-files)]
-                                ;; TODO: the `last-modified-at` attribute in the db is always after
-                                ;; the file system one because we transact to db first and write to the
-                                ;; file system then.
-                                ;; It doesn't mean this is a bug, but it could impact the performance.
-                                (> last-modified-at (:file/last-modified-at old-file))))
-                            new-files)
-                    (map :file/path))))
-     :deleted (set/difference old-diff new-diff)}))
+        new-diff (set/difference new-files old-files)
+        old-diff (set/difference old-files new-files)
+        added (set/difference new-diff old-diff)
+        deleted (set/difference old-diff new-diff)
+        modified (let [both-exist (set/difference new-diff added)]
+                   (when (seq both-exist)
+                     (->>
+                      (filter (fn [{:file/keys [path last-modified-at]}]
+                                (when-let [old-file (some #(when (= (:file/path %) path) %) old-files)]
+                                  ;; TODO: the `last-modified-at` attribute in the db is always after
+                                  ;; the file in the local file sytem because we transact to the db first and write to the
+                                  ;; file system later.
+                                  ;; It doesn't mean this is a bug, but it could impact the performance.
+                                  (> last-modified-at (:file/last-modified-at old-file))))
+                              both-exist)
+                      (map :file/path))))]
+    {:added added
+     :modified modified
+     :deleted deleted}))
 
 (defn- reload-dir!
   [repo]
   (when (and repo (config/local-db? repo))
-    (let [old-files (db/get-files-path-size-modified-at repo)
+    (let [old-files (db/get-files-full repo)
           dir-name (config/get-local-dir repo)
           handle-path (str config/local-handle-prefix dir-name)
           path-handles (atom {})]
@@ -159,16 +157,38 @@
                                              (swap! path-handles assoc path handle)))
               _ (set-files! @path-handles)
               new-files (->db-files dir-name files-result)
-              diffs (compute-diffs old-files new-files)]
-        ;; (-> (p/all (map (fn [file]
-        ;;                   (p/let [content (.text (:file/file file))]
-        ;;                     (assoc file :file/content content))) files-result))
-        ;;     (p/then (fn [result]
-        ;;               (let [files (map #(select-keys % [:file/path :file/content]) result)])))
-        ;;     (p/catch (fn [error]
-        ;;                (println "Load files content error: ")
-        ;;                (js/console.dir error))))
-        (prn {:diffs diffs})))))
+              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/path file)}) col))
+              diffs (concat
+                     (rename-f "remove" deleted)
+                     (rename-f "add" added)
+                     (rename-f "modify" modified))
+              _ (when (seq deleted)
+                  (p/all (map #(idb/remove-item! (str handle-path (:file/path %))) deleted)))
+              added-or-modified (set (concat added modified))
+              _ (when (seq added-or-modified)
+                  (p/all (map (fn [{:file/keys [path]}]
+                                (when-let [handle (get @path-handles path)]
+                                  (idb/set-item! (str handle-path path) handle))) added-or-modified)))]
+        (-> (p/all (map (fn [file]
+                          (when-let [file (get-file-f (:file/path file) new-files)]
+                            (p/let [content (.text (:file/file file))]
+                              (assoc file :file/content content)))) added-or-modified))
+            (p/then (fn [result]
+                      (let [files (->> (map #(dissoc % :file/file :file/handle) result)
+                                       (remove
+                                        (fn [file]
+                                          (let [content (:file/content file)
+                                                old-content (:file/content (get-file-f (:file/path file) old-files))]
+                                            (= content old-content)))))]
+                        (when (and (seq diffs) (seq files))
+                          (repo-handler/load-repo-to-db! repo
+                                                         {:diffs diffs
+                                                          :nfs-files files})))))
+            (p/catch (fn [error]
+                       (log/error :nfs/load-files-error error))))))))
 
 (defn- refresh!
   [repo]