Browse Source

fix: performance degradation

Tienson Qin 4 years ago
parent
commit
2585433740

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

@@ -61,7 +61,8 @@
             [reitit.frontend.easy :as rfe]
             [rum.core :as rum]
             [shadow.loader :as loader]
-            [frontend.components.query-table :as query-table]))
+            [frontend.components.query-table :as query-table]
+            [frontend.mixins :as mixins]))
 
 ;; TODO: remove rum/with-context because it'll make reactive queries not working
 
@@ -1414,7 +1415,8 @@
          bullet
 
          (or
-          (and empty-content? (not edit?)
+          (and empty-content?
+               (not edit?)
                (not (:block/top? block))
                (not (:block/bottom? block))
                (not (util/react *control-show?)))
@@ -1834,6 +1836,22 @@
                 (rum/with-key (block-child block)
                   (str uuid "-" idx)))))])]]]))
 
+(rum/defc block-refs-count < rum/reactive
+  [block]
+  (let [block-refs-count (model/get-block-refs-count (:block/uuid block))]
+    (when (and block-refs-count (> block-refs-count 0))
+      [:div
+       [:a.open-block-ref-link.bg-base-2.text-sm.ml-2.fade-link
+        {:title "Open block references"
+         :style {:margin-top -1}
+         :on-click (fn []
+                     (state/sidebar-add-block!
+                      (state/get-current-repo)
+                      (:db/id block)
+                      :block-ref
+                      {:block block}))}
+        block-refs-count]])))
+
 (rum/defc block-content-or-editor < rum/reactive
   [config {:block/keys [uuid title body meta content page format repo children marker properties pre-block? idx] :as block} edit-input-id block-id slide? heading-level edit?]
   (let [editor-box (get config :editor-box)
@@ -1888,19 +1906,7 @@
                          [:a.fade-link
                           summary]]))))
 
-        (let [block-refs-count (count (:block/_refs block))]
-          (when (and block-refs-count (> block-refs-count 0))
-            [:div
-             [:a.open-block-ref-link.bg-base-2.text-sm.ml-2.fade-link
-              {:title "Open block references"
-               :style {:margin-top -1}
-               :on-click (fn []
-                           (state/sidebar-add-block!
-                            (state/get-current-repo)
-                            (:db/id block)
-                            :block-ref
-                            {:block block}))}
-              block-refs-count]]))]])))
+        (block-refs-count block)]])))
 
 (defn non-dragging?
   [e]

+ 1 - 3
src/main/frontend/components/header.cljs

@@ -28,9 +28,7 @@
   []
   [:a.button
    {:href     (rfe/href :home)
-    :on-click (fn []
-                (util/scroll-to-top)
-                (state/set-journals-length! 2))}
+    :on-click route-handler/go-to-journals!}
    (ui/icon "home" {:style {:fontSize 20}})])
 
 (rum/defc login

+ 1 - 1
src/main/frontend/components/reference.cljs

@@ -66,7 +66,7 @@
          [page (map #(block-with-ref-level % 1) blocks)])
     page-blocks))
 
-(rum/defcs references < rum/reactive
+(rum/defcs references < rum/reactive db-mixins/query
   {:init (fn [state]
            (let [page-name (first (:rum/args state))
                  filters (when page-name

+ 2 - 2
src/main/frontend/components/sidebar.cljs

@@ -163,7 +163,7 @@
         [:li {:key name}
          (page-name name)])])))
 
-(rum/defc flashcards < rum/reactive
+(rum/defc flashcards < rum/reactive db-mixins/query
   []
   (let [num (srs/get-srs-cards-total)]
     [:a.item.group.flex.items-center.px-2.py-2.text-sm.font-medium.rounded-md {:on-click #(state/pub-event! [:modal/show-cards])}
@@ -184,7 +184,7 @@
          [:div.flex.flex-col.pb-4.wrap
           [:nav.flex-1.px-2.space-y-1 {:aria-label "Sidebar"}
            (repo/repos-dropdown)
-           [:a.item.group.flex.items-center.px-2.py-2.text-sm.font-medium.rounded-md {:href (rfe/href :all-journals)}
+           [:a.item.group.flex.items-center.px-2.py-2.text-sm.font-medium.rounded-md {:on-click route-handler/go-to-journals!}
             (ui/icon "calendar mr-3" {:style {:font-size 20}})
             [:span.flex-1 "Journals"]]
 

+ 12 - 1
src/main/frontend/db/model.cljs

@@ -449,6 +449,17 @@
             (recur sibling (rest next-siblings) (conj result sibling))
             result))))))
 
+(defn get-block-refs-count
+  [block-id]
+  (when-let [repo-url (state/get-current-repo)]
+    (when block-id
+      (some->
+      (react/q repo-url [:block/refs-count block-id]
+        {:query-fn (fn [db]
+                     (count (:block/_refs (db-utils/entity repo-url [:block/uuid block-id]))))}
+        nil)
+      react))))
+
 (defn get-page-blocks
   ([page]
    (get-page-blocks (state/get-current-repo) page nil))
@@ -456,7 +467,7 @@
    (get-page-blocks repo-url page nil))
   ([repo-url page {:keys [use-cache? pull-keys]
                    :or {use-cache? true
-                        pull-keys block-attrs}}]
+                        pull-keys '[*]}}]
    (let [page (string/lower-case (string/trim page))
          page-entity (or (db-utils/entity repo-url [:block/name page])
                          (db-utils/entity repo-url [:block/original-name page]))

+ 15 - 11
src/main/frontend/db/react.cljs

@@ -250,14 +250,17 @@
 
                              (apply concat
                                (for [{:block/keys [refs]} blocks]
-                                 (mapcat (fn [ref]
-                                           (when-let [block (if (and (map? ref) (:block/name ref))
-                                                              (db-utils/entity [:block/name (:block/name ref)])
-                                                              (db-utils/entity ref))]
-                                             [[:page/blocks (:db/id (:block/page block))]
-                                              ;; [:block/refed-blocks (:db/id block)]
-                                              ]))
-                                         refs))))
+                                 (map (fn [ref]
+                                        (cond
+                                          (and (map? ref) (:block/name ref))
+                                          [:page/blocks (:db/id (db-utils/entity [:block/name (:block/name ref)]))]
+
+                                          (and (vector? ref) (= (first ref) :block/uuid))
+                                          [:block/refs-count (second ref)]
+
+                                          :else
+                                          nil))
+                                   refs))))
                             (distinct))
               refed-pages (map
                            (fn [[k page-id]]
@@ -301,9 +304,10 @@
               (let [new-result (->
                                 (cond
                                   query-fn
-                                  (profile
-                                   "Query:"
-                                   (doall (query-fn db)))
+                                  (let [result (query-fn db)]
+                                    (if (coll? result)
+                                      (doall result)
+                                      result))
 
                                   inputs-fn
                                   (let [inputs (inputs-fn)]

+ 1 - 1
src/main/frontend/handler/route.cljs

@@ -115,7 +115,7 @@
 
 (defn go-to-journals!
   []
-  (state/set-journals-length! 2)
+  (state/set-journals-length! 1)
   (let [route (if (state/custom-home-page?)
                 :all-journals
                 :home)]