Skip to content

Commit

Permalink
fix: lazy load page/block embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed May 18, 2024
1 parent 936fe0b commit 4a681d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
43 changes: 21 additions & 22 deletions deps/db/src/logseq/db/sqlite/common_db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
[db block]
(update block :block/refs (fn [refs] (map (fn [ref] (d/pull db '[*] (:db/id ref))) refs))))

(defn- with-block-link
[db block]
(if (:block/link block)
(update block :block/link (fn [link] (d/pull db '[*] (:db/id link))))
block))

(defn with-parent
[db block]
(cond
Expand Down Expand Up @@ -148,12 +154,13 @@
get-children (fn [block children]
(let [long-page? (and (> (count children) 500) (not (contains? (:block/type block) "whiteboard")))]
(if long-page?
(map (fn [e]
(select-keys e [:db/id :block/uuid :block/page :block/order :block/parent :block/collapsed?]))
children)
(->> (map (fn [e]
(select-keys e [:db/id :block/uuid :block/page :block/order :block/parent :block/collapsed? :block/link]))
children)
(map #(with-block-link db %)))
(->> (d/pull-many db '[*] (map :db/id children))
(map #(with-block-refs db %))
(map mark-block-fully-loaded)
(map #(with-block-link db %))
(mapcat (fn [block]
(let [e (d/entity db (:db/id block))]
(conj
Expand All @@ -162,27 +169,19 @@
[])
block))))))))]
(when block
(if (:block/page block) ; not a page
(let [block' (->> (d/pull db '[*] (:db/id block))
(with-parent db)
(with-block-refs db)
mark-block-fully-loaded)]
(cond->
{:block block'
:properties (property-with-values db block)}
children?
(assoc :children (get-children block
(if nested-children?
(get-block-children db (:block/uuid block))
(:block/_parent block))))))
(let [block' (->> (d/pull db '[*] (:db/id block))
(with-parent db)
(with-block-refs db)
(with-block-link db)
mark-block-fully-loaded)]
(cond->
{:block (->> (d/pull db '[*] (:db/id block))
(with-tags db)
mark-block-fully-loaded)
{:block block'
:properties (property-with-values db block)}
children?
(assoc :children
(get-children block (:block/_page block))))))))
(assoc :children (get-children block
(if nested-children?
(get-block-children db (:block/uuid block))
(:block/_parent block)))))))))

(defn get-latest-journals
[db n]
Expand Down
11 changes: 5 additions & 6 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@
(rum/defc page-preview-trigger
[{:keys [children sidebar? tippy-position tippy-distance fixed-position? open? manual?] :as config} page-name]
(let [*tippy-ref (rum/create-ref)
page-name (util/page-name-sanity-lc page-name)
page-name (when page-name (util/page-name-sanity-lc page-name))
_ #_:clj-kondo/ignore (rum/defc html-template []
(let [*el-popup (rum/use-ref nil)]

Expand All @@ -663,7 +663,7 @@
#(.removeEventListener el-popup "keyup" cb)))
[])

(let [redirect-page-name (or (model/get-redirect-page-name page-name (:block/alias? config))
(let [redirect-page-name (or (and page-name (model/get-redirect-page-name page-name (:block/alias? config)))
page-name)]
(when redirect-page-name
[:div.tippy-wrapper.overflow-y-auto.p-4.outline-none.rounded-md
Expand Down Expand Up @@ -2836,11 +2836,10 @@
{:init (fn [state]
(let [*ref (atom nil)
block (nth (:rum/args state) 3)
block-id (:block/uuid block)
block-id (:db/id block)
repo (state/get-current-repo)]
(db-async/<get-block repo block-id :children? false)
(assoc state
::ref *ref)))}
(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)
_ (when (:block/uuid block) (state/sub-async-query-loading (:block/uuid block)))
Expand Down

0 comments on commit 4a681d7

Please sign in to comment.