Browse Source

chore: flatten promises

Tienson Qin 5 years ago
parent
commit
b5aad406aa
2 changed files with 35 additions and 32 deletions
  1. 1 0
      src/main/frontend/handler/external.cljs
  2. 34 32
      src/main/frontend/handler/file.cljs

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

@@ -29,6 +29,7 @@
                                          ".md")]
                            [path text]))))
                    files)]
+    ;; TODO: git add is quite slow
     (file-handler/alter-files repo files {:add-history? false
                                           :update-status? false
                                           :reset? true

+ 34 - 32
src/main/frontend/handler/file.cljs

@@ -141,38 +141,40 @@
                      reset? false}}]
    (let [files-tx (mapv (fn [[path content]]
                           (let [original-content (db/get-file-no-sub repo path)]
-                            [path original-content content])) files)]
-     (-> (p/all
-          (doall
-           (map
-            (fn [[path content]]
-              (if reset?
-                (db/reset-file! repo path content)
-                (db/set-file-content! repo path content))
-              (util/p-handle
-               (fs/write-file (util/get-repo-dir repo) path content)
-               (fn [_])
-               (fn [error]
-                 (println "Write file failed, path: " path ", content: " content)
-                 (js/console.error error))))
-            files)))
-         (p/then (fn [_result]
-                   (->
-                    (p/all
-                     (doall
-                      (map
-                       (fn [[path content]]
-                         (git-handler/git-add repo path update-status?))
-                       files)))
-                    (p/then (fn [_]
-                              (when git-add-cb
-                                (git-add-cb))))
-                    (p/catch (fn [error]
-                               (println "Git add failed")
-                               (js/console.error error))))
-                   (ui-handler/re-render-file!)
-                   (when add-history?
-                     (history/add-history! repo files-tx))))))))
+                            [path original-content content])) files)
+         write-file-f (fn [[path content]]
+                        (if reset?
+                          (db/reset-file! repo path content)
+                          (db/set-file-content! repo path content))
+                        (util/p-handle
+                         (fs/write-file (util/get-repo-dir repo) path content)
+                         (fn [_])
+                         (fn [error]
+                           (println "Write file failed, path: " path ", content: " content)
+                           (js/console.error error))))
+         git-add-f (fn [_result]
+                     (let [add-helper
+                           (fn []
+                             (doall
+                              (map
+                               (fn [[path content]]
+                                 (git-handler/git-add repo path update-status?))
+                               files)))]
+                       (-> (p/all (add-helper))
+                           (p/then (fn [_]
+                                     (when git-add-cb
+                                       (git-add-cb))))
+                           (p/catch (fn [error]
+                                      (println "Git add failed:")
+                                      (js/console.error error)))))
+                     (ui-handler/re-render-file!)
+                     (when add-history?
+                       (history/add-history! repo files-tx)))]
+     (-> (p/all (doall (map write-file-f files)))
+         (p/then git-add-f)
+         (p/catch (fn [error]
+                    (println "Alter files failed:")
+                    (js/console.error error)))))))
 
 (defn remove-file!
   [repo file]