fix: nfs diff resolve

feat/pwa
Tienson Qin 2020-12-02 00:35:46 +08:00
parent 9d9ce7f76a
commit 5563b1c28c
1 changed files with 23 additions and 21 deletions

View File

@ -124,22 +124,24 @@
set))) set)))
old-files (->set old-files ks) old-files (->set old-files ks)
new-files (->set new-files ks) new-files (->set new-files ks)
new-diff (set/difference new-files old-files) file-path-set-f (fn [col] (set (map :file/path col)))
old-diff (set/difference old-files new-files) get-file-f (fn [files path] (some #(when (= (:file/path %) path) %) files))
added (set/difference new-diff old-diff) old-file-paths (file-path-set-f old-files)
deleted (set/difference old-diff new-diff) new-file-paths (file-path-set-f new-files)
modified (let [both-exist (set/difference new-diff added)] added (set/difference new-file-paths old-file-paths)
(when (seq both-exist) deleted (set/difference old-file-paths new-file-paths)
(->> modified (let [modified (set/difference new-file-paths added)]
(filter (fn [{:file/keys [path last-modified-at]}] (when (seq modified)
(when-let [old-file (some #(when (= (:file/path %) path) %) old-files)] (filter (fn [path]
;; TODO: the `last-modified-at` attribute in the db is always after (let [old-file (get-file-f old-files path)
;; the file in the local file sytem because we transact to the db first and write to the new-file (get-file-f new-files path)]
;; file system later. ;; TODO: the `last-modified-at` attribute in the db is always after
;; It doesn't mean this is a bug, but it could impact the performance. ;; the file in the local file sytem because we transact to the db first and write to the
(> last-modified-at (:file/last-modified-at old-file)))) ;; file system later.
both-exist) ;; It doesn't mean this is a bug, but it could impact the performance.
(map :file/path))))] (> (:file/last-modified-at new-file)
(:file/last-modified-at old-file))))
modified)))]
{:added added {:added added
:modified modified :modified modified
:deleted deleted})) :deleted deleted}))
@ -160,20 +162,20 @@
get-file-f (fn [path files] (some #(when (= (:file/path %) path) %) files)) get-file-f (fn [path files] (some #(when (= (:file/path %) path) %) files))
{:keys [added modified deleted] :as diffs} (compute-diffs old-files new-files) {:keys [added modified deleted] :as diffs} (compute-diffs old-files new-files)
;; Use the same labels as isomorphic-git ;; Use the same labels as isomorphic-git
rename-f (fn [typ col] (mapv (fn [file] {:type typ :path (:file/path file)}) col)) rename-f (fn [typ col] (mapv (fn [file] {:type typ :path file}) col))
diffs (concat diffs (concat
(rename-f "remove" deleted) (rename-f "remove" deleted)
(rename-f "add" added) (rename-f "add" added)
(rename-f "modify" modified)) (rename-f "modify" modified))
_ (when (seq deleted) _ (when (seq deleted)
(p/all (map #(idb/remove-item! (str handle-path (:file/path %))) deleted))) (p/all (map #(idb/remove-item! (str handle-path %)) deleted)))
added-or-modified (set (concat added modified)) added-or-modified (set (concat added modified))
_ (when (seq added-or-modified) _ (when (seq added-or-modified)
(p/all (map (fn [{:file/keys [path]}] (p/all (map (fn [path]
(when-let [handle (get @path-handles path)] (when-let [handle (get @path-handles path)]
(idb/set-item! (str handle-path path) handle))) added-or-modified)))] (idb/set-item! (str handle-path path) handle))) added-or-modified)))]
(-> (p/all (map (fn [file] (-> (p/all (map (fn [path]
(when-let [file (get-file-f (:file/path file) new-files)] (when-let [file (get-file-f path new-files)]
(p/let [content (.text (:file/file file))] (p/let [content (.text (:file/file file))]
(assoc file :file/content content)))) added-or-modified)) (assoc file :file/content content)))) added-or-modified))
(p/then (fn [result] (p/then (fn [result]