reduce <get-block calls

Other things:
1. remove tag/property ref from `get-block-refs`.
2. pre-fetch property values
This commit is contained in:
Tienson Qin
2025-03-13 09:05:46 +08:00
parent b87c43e697
commit 2dce5eefd3
6 changed files with 44 additions and 21 deletions

View File

@@ -438,10 +438,21 @@
(defn get-block-refs
[db id]
(let [alias (->> (get-block-alias db id)
(let [entity (d/entity db id)
alias (->> (get-block-alias db id)
(cons id)
distinct)
refs (->> (mapcat (fn [id] (:block/_path-refs (d/entity db id))) alias)
refs (->> (mapcat (fn [id]
(->> (:block/_refs (d/entity db id))
(remove (fn [ref]
;; remove refs that have the block as either tag or property
(or (and
(class? entity)
(d/datom db :eavt (:db/id ref) :block/tags (:db/id entity)))
(and
(property? entity)
(d/datom db :eavt (:db/id ref) (:db/ident entity))))))))
alias)
distinct)]
(when (seq refs)
(d/pull-many db '[*] (map :db/id refs)))))

View File

@@ -5,6 +5,7 @@
[datascript.core :as d]
[datascript.impl.entity :as de]
[logseq.common.util :as common-util]
[logseq.db.common.sqlite :as sqlite-common-db]
[logseq.db.frontend.class :as db-class]
[logseq.db.frontend.entity-util :as entity-util]
[logseq.db.frontend.property :as db-property]
@@ -283,12 +284,12 @@
(take limit)
;; convert entity to map for serialization
(map (fn [e]
(cond->
(-> (into {} e)
(assoc
:id (:db/id e)
:db/id (:db/id e)))
all-pages?
(assoc :block.temp/refs-count (count (:block/_refs e)))))))]
(if all-pages?
{:block
(-> (into {} e)
(assoc
:db/id (:db/id e)
:block.temp/refs-count (count (:block/_refs e))))}
(sqlite-common-db/get-block-and-children db (:db/id e) {})))))]
{:count (count data*)
:data (vec data)})))))

View File

@@ -3391,10 +3391,12 @@
(rum/defcs ^:large-vars/cleanup-todo block-container-inner < rum/reactive db-mixins/query
{:init (fn [state]
(let [*ref (atom nil)
block (nth (:rum/args state) 3)
args (:rum/args state)
[_state _ config block] args
block-id (:db/id block)
repo (state/get-current-repo)]
(db-async/<get-block repo block-id :children? true)
(when-not (or (:table-view? config) (:property-block? config))
(db-async/<get-block repo block-id :children? true))
(assoc state ::ref *ref)))}
[state container-state repo config* block {:keys [navigating-block navigated?]}]
(let [*ref (::ref state)

View File

@@ -956,11 +956,6 @@
(property-empty-btn-value property)])))
(rum/defcs property-block-value < rum/reactive db-mixins/query
{:init (fn [state]
(let [block (first (:rum/args state))]
(when-let [block-id (or (:db/id block) (:block/uuid block))]
(db-async/<get-block (state/get-current-repo) block-id :children? true)))
state)}
[state value block property page-cp]
(when value
(if (state/sub-async-query-loading (:block/uuid value))

View File

@@ -213,6 +213,7 @@
(rum/defcs references* < rum/reactive db-mixins/query
(rum/local nil ::ref-pages)
{:init (fn [state]
;; TODO: move refs
(let [page (first (:rum/args state))]
(when page (db-async/<get-block-refs (state/get-current-repo) (:db/id page))))
(assoc state ::filters (atom nil)))}

View File

@@ -6,6 +6,7 @@
[cljs-time.format :as tf]
[clojure.set :as set]
[clojure.string :as string]
[datascript.core :as d]
[datascript.impl.entity :as de]
[dommy.core :as dom]
[frontend.components.dnd :as dnd]
@@ -212,7 +213,9 @@
:cell (fn [table row _column]
(block-container (assoc config
:raw-title? (ldb/asset? row)
:table? true)
;; TODO: remove this
:table? true
:table-view? true)
row
table))
:disable-hide? true})]
@@ -1572,10 +1575,20 @@
(defn- <load-view-data
[view offset]
(p/let [data-str (.get-view-data ^js @state/*db-worker (state/get-current-repo) (:db/id view)
(ldb/write-transit-str {:offset offset :limit 100}))
data (ldb/read-transit-str data-str)]
data))
(p/let [feature-type (:logseq.property.view/feature-type view)
data-str (.get-view-data ^js @state/*db-worker (state/get-current-repo) (:db/id view)
(ldb/write-transit-str {:offset offset :limit 50}))
{:keys [count data]} (ldb/read-transit-str data-str)
blocks (mapv :block data)
property-values (mapcat :properties data)]
(when-not (= feature-type :all-pages)
(let [blocks' (->> (concat property-values blocks)
(remove (fn [b]
(:block.temp/fully-loaded? (db/entity (:db/id b))))))]
(when (seq blocks')
(d/transact! (db/get-db false) blocks'))))
{:count count
:data blocks}))
(rum/defc view < rum/static
[{:keys [view-parent view-feature-type view-entity] :as option}]