Browse Source

perf enhancements

1. group transaction for save-block and insert-new-block when pressing
Enter
2. async store to sqlite
Tienson Qin 1 year ago
parent
commit
89d852b167
2 changed files with 18 additions and 8 deletions
  1. 16 6
      src/main/frontend/db_worker.cljs
  2. 2 2
      src/main/frontend/handler/editor.cljs

+ 16 - 6
src/main/frontend/db_worker.cljs

@@ -31,6 +31,7 @@
 (defonce *datascript-conns worker-state/*datascript-conns)
 (defonce *opfs-pools worker-state/*opfs-pools)
 (defonce *publishing? (atom false))
+(defonce *store-jobs (atom #{}))
 
 (defn- get-pool-name
   [graph-name]
@@ -104,13 +105,15 @@
   [repo _opts]
   (reify IStorage
     (-store [_ addr+data-seq delete-addrs]
-      (prn :debug (str "SQLite store addr+data count: " (count addr+data-seq)))
       (let [data (map
                   (fn [[addr data]]
                     #js {:$addr addr
                          :$content (pr-str data)})
-                  addr+data-seq)]
-        (upsert-addr-content! repo data delete-addrs)))
+                  addr+data-seq)
+            p (p/do! (upsert-addr-content! repo data delete-addrs))]
+        (swap! *store-jobs conj p)
+        (p/then p (fn [] (swap! *store-jobs disj p)))
+        p))
 
     (-restore [_ addr]
       (restore-data-from-addr repo addr))))
@@ -260,9 +263,16 @@
   (createOrOpenDB
    [_this repo & {:keys [close-other-db?]
                   :or {close-other-db? true}}]
-   (p/let [_ (when close-other-db?
-               (close-other-dbs! repo))]
-     (create-or-open-db! repo)))
+   (p/do!
+    ;; Store the current db if store jobs not finished yet
+    (when (seq @*store-jobs)
+      (-> (p/all @*store-jobs)
+          (p/then (fn [_]
+                    (reset! *store-jobs #{})
+                    (println "DB store job finished")))))
+    (when close-other-db?
+      (close-other-dbs! repo))
+    (create-or-open-db! repo)))
 
   (getMaxTx
    [_this repo]

+ 2 - 2
src/main/frontend/handler/editor.cljs

@@ -345,9 +345,9 @@
                    :else
                    (not has-children?))]
     (p/do!
-     (save-current-block! {:current-block current-block})
      (ui-outliner-tx/transact!
       {:outliner-op :insert-blocks}
+       (save-current-block! {:current-block current-block})
        (outliner-core/insert-blocks! (state/get-current-repo) (db/get-db false)
                                     [new-block] current-block {:sibling? sibling?
                                                                :keep-uuid? keep-uuid?
@@ -408,7 +408,7 @@
                        (wrap-parse-block))
         sibling? (when block-self? false)]
     (p/let [_ (outliner-insert-block! config current-block next-block {:sibling? sibling?
-                                                              :keep-uuid? true})]
+                                                                       :keep-uuid? true})]
       (util/set-change-value input fst-block-text)
       (assoc next-block :block/content snd-block-text))))