Browse Source

enhance: validate outliner data when get-block-children-ids

rcmerci 3 years ago
parent
commit
838ac93b69
1 changed files with 14 additions and 8 deletions
  1. 14 8
      src/main/frontend/db/model.cljs

+ 14 - 8
src/main/frontend/db/model.cljs

@@ -890,18 +890,24 @@ independent of format as format specific heading characters are stripped"
                               '[:db/id :block/name :block/original-name]
                               ids))))))
 
-
 (defn get-block-children-ids
   [repo block-uuid]
   (when-let [db (conn/get-db repo)]
     (when-let [eid (:db/id (db-utils/entity repo [:block/uuid block-uuid]))]
-      (let [get-children-ids (fn get-children-ids [eid]
-                               (mapcat
-                                (fn [datom]
-                                  (let [id (first datom)]
-                                    (cons (:block/uuid (db-utils/entity db id)) (get-children-ids id))))
-                                (d/datoms db :avet :block/parent eid)))]
-        (get-children-ids eid)))))
+      (let [seen   (volatile! [])]
+        (loop [steps          100               ;check result every 100 steps
+               eids-to-expand [eid]]
+          (when (seq eids-to-expand)
+            (let [eids-to-expand*
+                  (mapcat (fn [eid] (map first (d/datoms db :avet :block/parent eid))) eids-to-expand)
+                  uuids-to-add (remove nil? (map #(:block/uuid (db-utils/entity db %)) eids-to-expand*))]
+              (when (and (zero? steps)
+                         (seq (set/intersection (set @seen) (set uuids-to-add))))
+                (throw (ex-info "bad outliner data, need to re-index to fix"
+                                {:seen @seen :eids-to-expand eids-to-expand})))
+              (vswap! seen (partial apply conj) uuids-to-add)
+              (recur (if (zero? steps) 100 (dec steps)) eids-to-expand*))))
+        @seen))))
 
 (defn get-block-immediate-children
   "Doesn't include nested children."