Skip to content

Commit 9396be6

Browse files
committed
refactor: update dependency registration logic in RscRenderer
- Changed the parameter type of `register_dependency_if_needed` and `auto_register_component_from_fs` from `&str` to `String` for better ownership management. - Improved dependency registration by cloning dependencies where necessary to avoid borrowing issues. - Streamlined the handling of sub-dependencies during registration to enhance performance and clarity.
1 parent 85668c5 commit 9396be6

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

crates/rari/src/rsc/renderer.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ impl RscRenderer {
405405
let dependencies = extract_dependencies(component_code);
406406

407407
for dep in &dependencies {
408-
if let Err(e) = self.register_dependency_if_needed(dep).await {
408+
let dep_owned = dep.clone();
409+
if let Err(e) = Box::pin(self.register_dependency_if_needed(dep_owned.clone())).await {
409410
error!(
410411
"[RSC] Warning: Failed to register dependency '{dep}' for component '{component_id}': {e}"
411412
);
@@ -522,7 +523,7 @@ impl RscRenderer {
522523
|| (has_react_import && has_component_export)
523524
}
524525

525-
async fn register_dependency_if_needed(&mut self, dep: &str) -> Result<(), RariError> {
526+
async fn register_dependency_if_needed(&mut self, dep: String) -> Result<(), RariError> {
526527
if !dep.starts_with("./") && !dep.starts_with("../") {
527528
return Ok(());
528529
}
@@ -595,7 +596,7 @@ impl RscRenderer {
595596
if !already_registered {
596597
if Self::is_react_component_file(&content) {
597598
let sub_dependencies = extract_dependencies(&content);
598-
for sub_dep in &sub_dependencies {
599+
for sub_dep in sub_dependencies {
599600
let _ = Box::pin(self.register_dependency_if_needed(sub_dep)).await;
600601
}
601602

@@ -1401,14 +1402,14 @@ impl RscRenderer {
14011402
if self.component_exists(candidate) {
14021403
return Ok(candidate.clone());
14031404
}
1404-
if self.auto_register_component_from_fs(candidate).await.is_ok() {
1405+
if self.auto_register_component_from_fs(candidate.clone()).await.is_ok() {
14051406
return Ok(candidate.clone());
14061407
}
14071408
}
14081409

14091410
sleep(Duration::from_millis(20)).await;
14101411
if self.component_exists(original_id)
1411-
|| self.auto_register_component_from_fs(original_id).await.is_ok()
1412+
|| self.auto_register_component_from_fs(original_id.to_string()).await.is_ok()
14121413
{
14131414
return Ok(original_id.to_string());
14141415
}
@@ -1509,7 +1510,7 @@ impl RscRenderer {
15091510
Ok(())
15101511
}
15111512

1512-
async fn auto_register_component_from_fs(&self, component_id: &str) -> Result<(), RariError> {
1513+
async fn auto_register_component_from_fs(&self, component_id: String) -> Result<(), RariError> {
15131514
if let Some(config) = crate::server::config::Config::get()
15141515
&& !config.is_development()
15151516
{
@@ -1524,7 +1525,7 @@ impl RscRenderer {
15241525
cwd.join("docs/src/pages"),
15251526
];
15261527

1527-
let stem = component_id;
1528+
let stem = component_id.clone();
15281529
let exts = [".tsx", ".ts", ".jsx", ".js"];
15291530
let mut found: Option<std::path::PathBuf> = None;
15301531
for root in &search_roots {
@@ -1535,7 +1536,7 @@ impl RscRenderer {
15351536
break;
15361537
}
15371538

1538-
let candidate_idx = root.join(stem).join(format!("index{ext}"));
1539+
let candidate_idx = root.join(stem.clone()).join(format!("index{ext}"));
15391540
if candidate_idx.exists() {
15401541
found = Some(candidate_idx);
15411542
break;
@@ -1565,7 +1566,7 @@ impl RscRenderer {
15651566
resource_tracker: Arc::clone(&self.resource_tracker),
15661567
serializer: Arc::clone(&self.serializer),
15671568
};
1568-
renderer.register_component(component_id, &code).await?;
1569+
renderer.register_component(&component_id, &code).await?;
15691570
}
15701571
Ok(())
15711572
}

0 commit comments

Comments
 (0)