Skip to content

Commit 56ae552

Browse files
committed
fix: avoid infinite recusion due to cyclic dependency
1 parent c2442a9 commit 56ae552

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

collab-folder/src/folder.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::borrow::{Borrow, BorrowMut};
2-
use std::collections::HashMap;
2+
use std::collections::{HashMap, HashSet};
33
use std::ops::{Deref, DerefMut};
44
use std::sync::Arc;
55

@@ -406,7 +406,9 @@ impl Folder {
406406
/// * `Vec<View>`: A vector of `View` objects that includes the parent view and all of its child views.
407407
pub fn get_view_recursively(&self, view_id: &str) -> Vec<View> {
408408
let txn = self.collab.transact();
409-
self.body.get_view_recursively_with_txn(&txn, view_id)
409+
self
410+
.body
411+
.get_view_recursively_with_txn(&txn, view_id, &mut HashSet::default())
410412
}
411413
}
412414

@@ -596,7 +598,15 @@ impl FolderBody {
596598
/// # Returns
597599
///
598600
/// * `Vec<View>`: A vector of `View` objects that includes the parent view and all of its child views.
599-
pub fn get_view_recursively_with_txn<T: ReadTxn>(&self, txn: &T, view_id: &str) -> Vec<View> {
601+
pub fn get_view_recursively_with_txn<T: ReadTxn>(
602+
&self,
603+
txn: &T,
604+
view_id: &str,
605+
visited: &mut HashSet<String>,
606+
) -> Vec<View> {
607+
if !visited.insert(view_id.to_string()) {
608+
return vec![];
609+
}
600610
match self.views.get_view_with_txn(txn, view_id) {
601611
None => vec![],
602612
Some(parent_view) => {
@@ -605,7 +615,7 @@ impl FolderBody {
605615
.children
606616
.items
607617
.iter()
608-
.flat_map(|child| self.get_view_recursively_with_txn(txn, &child.id))
618+
.flat_map(|child| self.get_view_recursively_with_txn(txn, &child.id, visited))
609619
.collect::<Vec<_>>();
610620
views.extend(child_views);
611621
views
@@ -643,7 +653,7 @@ impl FolderBody {
643653
.map(|view| view.as_ref().clone())
644654
.collect::<Vec<View>>();
645655
for view in self.views.get_views_belong_to(txn, workspace_id) {
646-
views.extend(self.get_view_recursively_with_txn(txn, &view.id));
656+
views.extend(self.get_view_recursively_with_txn(txn, &view.id, &mut HashSet::default()));
647657
}
648658
views.extend(orphan_views);
649659

0 commit comments

Comments
 (0)