fix: performance degrading for nested linked references

Fix #6316.

This commit also fixed the issue that toggle block children doesn't
work sometimes.
This commit is contained in:
Tienson Qin
2022-08-11 16:36:19 +08:00
parent 471b52b985
commit eeb827a1d3
5 changed files with 107 additions and 40 deletions

View File

@@ -68,14 +68,15 @@
(defn- tree [flat-nodes root-id]
(let [children (group-by :block/parent flat-nodes)
nodes (fn nodes [parent-id]
(map (fn [b]
(let [children (nodes (:db/id b))]
(if (seq children)
(assoc b :block/children children)
b)))
(children {:db/id parent-id})))]
(nodes root-id)))
nodes (fn nodes [parent-id level]
(mapv (fn [b]
(let [b' (assoc b :block/level (inc level))
children (nodes (:db/id b) (inc level))]
(if (seq children)
(assoc b' :block/children children)
b')))
(get children {:db/id parent-id})))]
(nodes root-id 1)))
(defn non-consecutive-blocks->vec-tree
"`blocks` need to be in the same page."
@@ -88,12 +89,15 @@
id->parent (zipmap (map :db/id blocks)
(map (comp :db/id :block/parent) blocks))
top-level-ids (set (remove #(id->parent (id->parent %)) (map :db/id blocks)))
;; Separate blocks into parent and children groups [parent-children, parent-children]
blocks' (loop [blocks blocks
result []]
(if-let [block (first blocks)]
(if (top-level-ids (:db/id block))
(recur (rest blocks) (conj result [block]))
(recur (rest blocks) (conj (vec (butlast result)) (conj (last result) block))))
(let [block' (assoc block :block/level 1)]
(recur (rest blocks) (conj result [block'])))
(recur (rest blocks) (conj (vec (butlast result))
(conj (last result) block))))
result))]
(map (fn [[parent & children]]
(if (seq children)