فهرست منبع

avoid async queries if blocks are not rendered yet

Tienson Qin 1 سال پیش
والد
کامیت
d00b2c1e47

+ 3 - 7
deps/outliner/src/logseq/outliner/datascript.cljs

@@ -76,9 +76,8 @@
     txs))
     txs))
 
 
 (defn transact!
 (defn transact!
-  [txs tx-meta {:keys [repo conn unlinked-graph? set-state-fn]}]
-  (let [db-based? (and repo (sqlite-util/db-based-graph? repo))
-        txs (map (fn [m]
+  [txs tx-meta {:keys [repo conn set-state-fn]}]
+  (let [txs (map (fn [m]
                    (if (map? m)
                    (if (map? m)
                      (dissoc m :block/children :block/meta :block/top? :block/bottom? :block/anchor
                      (dissoc m :block/children :block/meta :block/top? :block/bottom? :block/anchor
                              :block/title :block/body :block/level :block/container :db/other-tx
                              :block/title :block/body :block/level :block/container :db/other-tx
@@ -92,10 +91,7 @@
               true
               true
               (distinct))]
               (distinct))]
 
 
-    (when (and (seq txs)
-               (or db-based?
-                   (and (fn? unlinked-graph?) (not (unlinked-graph?)))
-                   (some? js/process)))
+    (when (seq txs)
 
 
       ;; (prn :debug "DB transact")
       ;; (prn :debug "DB transact")
       ;; (cljs.pprint/pprint txs)
       ;; (cljs.pprint/pprint txs)

+ 24 - 15
src/main/frontend/components/block.cljs

@@ -2945,7 +2945,18 @@
                  *hidden? (get-hidden-atom id *ref
                  *hidden? (get-hidden-atom id *ref
                                            {:initial-value (when (or disable-lazy? editing?) false)
                                            {:initial-value (when (or disable-lazy? editing?) false)
                                             :id (:db/id current-block)
                                             :id (:db/id current-block)
-                                            :content (:block/content current-block)})]
+                                            :content (:block/content current-block)})
+                 <load-block (fn []
+                               (let [block-id (:block/uuid (nth (:rum/args state) 3))]
+                                 (db-async/<get-block (state/get-current-repo) block-id :children? false)))]
+             (if (false? @*hidden?)
+               (<load-block)
+               (add-watch *hidden?
+                        :show
+                        (fn [_ _ _ n]
+                          (when (false? n)
+                            (<load-block)))))
+
              (assoc state
              (assoc state
                     ::sub-id id
                     ::sub-id id
                     ::ref *ref
                     ::ref *ref
@@ -2977,6 +2988,11 @@
   (let [*ref (::ref state)
   (let [*ref (::ref state)
         ref (rum/react *ref)
         ref (rum/react *ref)
         hidden? (rum/react (::hidden? state))
         hidden? (rum/react (::hidden? state))
+        _ (when (:block/uuid block) (state/sub-async-query-loading (:block/uuid block)))
+        [original-block block] (build-block config* block {:navigating-block navigating-block :navigated? navigated?})
+        config* (if original-block
+                  (assoc config* :original-block original-block)
+                  config*)
         ref? (:ref? config*)
         ref? (:ref? config*)
         ;; whiteboard block shape
         ;; whiteboard block shape
         in-whiteboard? (and (:in-whiteboard? config*)
         in-whiteboard? (and (:in-whiteboard? config*)
@@ -3079,8 +3095,8 @@
                            (block-mouse-leave e *control-show? block-id doc-mode?))}
                            (block-mouse-leave e *control-show? block-id doc-mode?))}
         (when (and (not slide?) (not in-whiteboard?) (not hidden?))
         (when (and (not slide?) (not in-whiteboard?) (not hidden?))
           (let [edit? (or edit?
           (let [edit? (or edit?
-                             (= uuid (:block/uuid (state/get-edit-block)))
-                             (contains? @(:editor/new-created-blocks @state/state) uuid))]
+                          (= uuid (:block/uuid (state/get-edit-block)))
+                          (contains? @(:editor/new-created-blocks @state/state) uuid))]
             (block-control config block uuid block-id collapsed? *control-show? edit? selected?)))
             (block-control config block uuid block-id collapsed? *control-show? edit? selected?)))
 
 
         (when (and @*show-left-menu? (not in-whiteboard?) (not hidden?))
         (when (and @*show-left-menu? (not in-whiteboard?) (not hidden?))
@@ -3125,7 +3141,6 @@
   {:init (fn [state]
   {:init (fn [state]
            (let [[config block] (:rum/args state)
            (let [[config block] (:rum/args state)
                  block-id (:block/uuid block)]
                  block-id (:block/uuid block)]
-             (db-async/<get-block (state/get-current-repo) block-id :children? false)
              (cond
              (cond
                (root-block? config block)
                (root-block? config block)
                (state/set-collapsed-block! block-id false)
                (state/set-collapsed-block! block-id false)
@@ -3150,18 +3165,12 @@
   (let [repo (state/get-current-repo)
   (let [repo (state/get-current-repo)
         *navigating-block (get state ::navigating-block)
         *navigating-block (get state ::navigating-block)
         navigating-block (rum/react *navigating-block)
         navigating-block (rum/react *navigating-block)
-        navigated? (and (not= (:block/uuid block) navigating-block) navigating-block)
-        _ (when (:block/uuid block) (state/sub-async-query-loading (:block/uuid block)))]
+        navigated? (and (not= (:block/uuid block) navigating-block) navigating-block)]
     (when (:block/uuid block)
     (when (:block/uuid block)
-      (let [[original-block block] (build-block config block {:navigating-block navigating-block :navigated? navigated?})
-            config' (if original-block
-                      (assoc config :original-block original-block)
-                      config)
-            opts {}]
-        (rum/with-key
-          (block-container-inner state repo config' block
-                                 (merge opts {:navigating-block navigating-block :navigated? navigated?}))
-          (str "block-inner" (:block/uuid block)))))))
+      (rum/with-key
+        (block-container-inner state repo config block
+                               {:navigating-block navigating-block :navigated? navigated?})
+        (str "block-inner" (:block/uuid block))))))
 
 
 (defn divide-lists
 (defn divide-lists
   [[f & l]]
   [[f & l]]

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

@@ -117,6 +117,7 @@
     (<get-db-based-property-values graph property)
     (<get-db-based-property-values graph property)
     (file-async/<get-file-based-property-values graph property)))
     (file-async/<get-file-based-property-values graph property)))
 
 
+;; TODO: batch queries for better performance and UX
 (defn <get-block
 (defn <get-block
   [graph name-or-uuid & {:keys [children?]
   [graph name-or-uuid & {:keys [children?]
                          :or {children? true}}]
                          :or {children? true}}]
@@ -152,7 +153,7 @@
     (p/let [result-str (.get-right-sibling worker graph db-id)
     (p/let [result-str (.get-right-sibling worker graph db-id)
             result (edn/read-string result-str)
             result (edn/read-string result-str)
             conn (db/get-db graph false)
             conn (db/get-db graph false)
-            _ (d/transact! conn result)]
+            _ (when result (d/transact! conn [result]))]
       result)))
       result)))
 
 
 (defn <get-block-parents
 (defn <get-block-parents

+ 4 - 1
src/main/frontend/db/async/util.cljs

@@ -20,7 +20,10 @@
                                      (not (map? (first result'))))
                                      (not (map? (first result'))))
                               (apply concat result')
                               (apply concat result')
                               result')]
                               result')]
-                (d/transact! conn tx-data))))
+                (try
+                  (d/transact! conn tx-data)
+                  (catch :default _e
+                    nil)))))
           result')))))
           result')))))
 
 
 (defn <pull
 (defn <pull