Browse Source

use :block/_refs instead of reactive block-refs-count query

Tienson Qin 3 years ago
parent
commit
06b15bdfc3
2 changed files with 8 additions and 20 deletions
  1. 6 13
      src/main/frontend/db/model.cljs
  2. 2 7
      src/main/frontend/db/react.cljs

+ 6 - 13
src/main/frontend/db/model.cljs

@@ -408,17 +408,6 @@
   ([repo block-id]
    (some? (:block/_parent (db-utils/entity repo [:block/uuid block-id])))))
 
-(defn get-block-refs-count
-  [block-db-id]
-  (when-let [repo-url (state/get-current-repo)]
-    (when block-db-id
-      (some->
-       (react/q repo-url [:frontend.db.react/block-refs-count block-db-id]
-         {:query-fn (fn [_db _tx-report _result]
-                      (count (:block/_refs (db-utils/entity repo-url block-db-id))))}
-         nil)
-       react))))
-
 (defn- collapsed-and-has-children?
   [block]
   (and (:block/collapsed? block) (has-children? (:block/uuid block))))
@@ -530,7 +519,7 @@
   ([repo-url block-id]
    (get-paginated-blocks repo-url block-id {}))
   ([repo-url block-id {:keys [pull-keys start-block limit use-cache? scoped-block-id]
-                       :or {pull-keys '[*]
+                       :or {pull-keys '[* :block/_refs]
                             limit 50
                             use-cache? true
                             scoped-block-id nil}}]
@@ -550,7 +539,11 @@
       (react/q repo-url [query-key block-id]
         {:use-cache? use-cache?
          :query-fn (fn [db tx-report result]
-                     (let [tx-block-ids (distinct (mapv :e (:tx-data tx-report)))
+                     (let [tx-data (:tx-data tx-report)
+                           refs (some->> (filter #(= :block/refs (:a %)) tx-data)
+                                         (map :v))
+                           tx-block-ids (distinct (->> (map :e tx-data)
+                                                       (concat refs)))
                            [tx-id->block cached-id->block] (when (and tx-report result)
                                                              (let [blocks (->> (db-utils/pull-many repo-url pull-keys tx-block-ids)
                                                                                (remove nil?))]

+ 2 - 7
src/main/frontend/db/react.cljs

@@ -10,7 +10,6 @@
             [frontend.db.utils :as db-utils]
             [frontend.state :as state]
             [frontend.util :as util :refer [react]]
-            [frontend.db-schema :as db-schema]
             [cljs.spec.alpha :as s]
             [clojure.core.async :as async]))
 
@@ -18,9 +17,6 @@
 ;; ::block
 ;; pull-block react-query
 (s/def ::block (s/tuple #(= ::block %) uuid?))
-;; ::block-refs-count
-;; (count (:block/refs block)) react-query
-(s/def ::block-refs-count (s/tuple #(= ::block-refs-count %) int?))
 ;; ::page-blocks
 ;; get page-blocks react-query
 (s/def ::page-blocks (s/tuple #(= ::page-blocks %) int?))
@@ -49,7 +45,6 @@
 (s/def ::custom any?)
 
 (s/def ::react-query-keys (s/or :block ::block
-                                :block-refs-count ::block-refs-count
                                 :page-blocks ::page-blocks
                                 :block-and-children ::block-and-children
                                 :journals ::journals
@@ -254,7 +249,7 @@
                               (let [entity (db-utils/entity ref)]
                                 (if (:block/name entity) ; page
                                   [::page-blocks ref]
-                                  [::block-refs-count ref])))
+                                  [::page-blocks (:db/id (:block/page entity))])))
                             refs))
         others (->>
                 (keys @query-state)
@@ -311,7 +306,7 @@
                  (or (get affected-keys (vec (rest k)))
                      custom?
                      kv?))
-            (let [{:keys [query query-fn result]} cache]
+            (let [{:keys [query query-fn]} cache]
               (when (or query query-fn)
                 (try
                   (let [f #(execute-query! repo-url db k tx cache)]