瀏覽代碼

perf: delay writes for long page

Tienson Qin 3 年之前
父節點
當前提交
5aa0db2f21
共有 2 個文件被更改,包括 19 次插入10 次删除
  1. 12 8
      src/main/frontend/modules/outliner/file.cljs
  2. 7 2
      src/main/frontend/modules/outliner/pipeline.cljs

+ 12 - 8
src/main/frontend/modules/outliner/file.cljs

@@ -25,14 +25,18 @@
   [repo page-db-id]
   (let [page-block (db/pull repo '[*] page-db-id)
         page-db-id (:db/id page-block)
-        blocks (model/get-page-blocks-no-cache repo (:block/name page-block))]
-    (when-not (and (= 1 (count blocks))
-                   (string/blank? (:block/content (first blocks)))
-                   (nil? (:block/file page-block)))
-      (let [tree (tree/blocks->vec-tree repo blocks (:block/name page-block))]
-        (if page-block
-          (file/save-tree page-block tree)
-          (js/console.error (str "can't find page id: " page-db-id)))))))
+        blocks-count (model/get-page-blocks-count repo page-db-id)]
+    (if (and (> blocks-count 500)
+             (not (state/input-idle? repo :diff 3000)))           ; long page
+      (async/put! write-chan [repo page-db-id])
+      (let [blocks (model/get-page-blocks-no-cache repo (:block/name page-block))]
+        (when-not (and (= 1 (count blocks))
+                       (string/blank? (:block/content (first blocks)))
+                       (nil? (:block/file page-block)))
+          (let [tree (tree/blocks->vec-tree repo blocks (:block/name page-block))]
+            (if page-block
+              (file/save-tree page-block tree)
+              (js/console.error (str "can't find page id: " page-db-id)))))))))
 
 (defn write-files!
   [pages]

+ 7 - 2
src/main/frontend/modules/outliner/pipeline.cljs

@@ -23,7 +23,10 @@
 (defn compute-block-path-refs
   [tx-meta blocks]
   (let [repo (state/get-current-repo)
-        blocks (remove :block/name blocks)]
+        blocks (remove :block/name blocks)
+        blocks (if (= (:outliner-op tx-meta) :insert-blocks)
+                 (butlast blocks)
+                 blocks)]
     (when (:outliner-op tx-meta)
       (when (react/path-refs-need-recalculated? tx-meta)
         (let [*computed-ids (atom #{})]
@@ -61,7 +64,9 @@
                (not (:compute-new-refs? tx-meta)))
       (let [{:keys [pages blocks]} (ds-report/get-blocks-and-pages tx-report)
             repo (state/get-current-repo)
-            refs-tx (set (compute-block-path-refs (:tx-meta tx-report) blocks))
+            refs-tx (util/profile
+                     "Compute path refs: "
+                     (set (compute-block-path-refs (:tx-meta tx-report) blocks)))
             truncate-refs-tx (map (fn [m] [:db/retract (:db/id m) :block/path-refs]) refs-tx)
             tx (util/concat-without-nil truncate-refs-tx refs-tx)
             tx-report' (if (seq tx)