Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

refactor: optimize enhanced_path function with OnceLock for caching

Tonho133743abdf8a
diff --git a/src-tauri/src/core/process.rs b/src-tauri/src/core/process.rs
index a57173ff..699adb59 100644
--- a/src-tauri/src/core/process.rs
+++ b/src-tauri/src/core/process.rs
@@ -1,26 +1,32 @@
fn enhanced_path() -> Option<String> {
- let bin_dir = crate::core::paths::app_data_dir()?.join("bin");
- let sep = if cfg!(windows) { ";" } else { ":" };
- let current = std::env::var("PATH").unwrap_or_default();
+ use std::sync::OnceLock;
+ static CACHED: OnceLock<Option<String>> = OnceLock::new();
+ CACHED
+ .get_or_init(|| {
+ let bin_dir = crate::core::paths::app_data_dir()?.join("bin");
+ let sep = if cfg!(windows) { ";" } else { ":" };
+ let current = std::env::var("PATH").unwrap_or_default();
- #[allow(unused_mut)]
- let mut extra_dirs: Vec<String> = vec![bin_dir.display().to_string()];
+ #[allow(unused_mut)]
+ let mut extra_dirs: Vec<String> = vec![bin_dir.display().to_string()];
- #[cfg(target_os = "macos")]
- {
- extra_dirs.push("/opt/homebrew/bin".into());
- extra_dirs.push("/usr/local/bin".into());
- }
+ #[cfg(target_os = "macos")]
+ {
+ extra_dirs.push("/opt/homebrew/bin".into());
+ extra_dirs.push("/usr/local/bin".into());
+ }
- #[cfg(target_os = "linux")]
- {
- if let Some(home) = dirs::home_dir() {
- extra_dirs.push(home.join(".local").join("bin").display().to_string());
- }
- extra_dirs.push("/usr/local/bin".into());
- }
+ #[cfg(target_os = "linux")]
+ {
+ if let Some(home) = dirs::home_dir() {
+ extra_dirs.push(home.join(".local").join("bin").display().to_string());
+ }
+ extra_dirs.push("/usr/local/bin".into());
+ }
- Some(format!("{}{}{}", extra_dirs.join(sep), sep, current))
+ Some(format!("{}{}{}", extra_dirs.join(sep), sep, current))
+ })
+ .clone()
}
pub fn command<S: AsRef<std::ffi::OsStr>>(program: S) -> tokio::process::Command {