Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

don't use static ref for in_flight_bases + cleanup and update comments

Ilya09025351f951
diff --git a/apps/desktop/src-tauri/src/update_project_names.rs b/apps/desktop/src-tauri/src/update_project_names.rs
index d5f7f908d..f52737ab7 100644
--- a/apps/desktop/src-tauri/src/update_project_names.rs
+++ b/apps/desktop/src-tauri/src/update_project_names.rs
@@ -1,23 +1,18 @@
use std::{
collections::HashSet,
path::{Path, PathBuf},
+ sync::Arc,
};
use cap_project::RecordingMeta;
use futures::StreamExt;
-use lazy_static::lazy_static;
use tauri::AppHandle;
-use tokio::fs;
+use tokio::{fs, sync::Mutex};
use crate::recordings_path;
const STORE_KEY: &str = "uuid_projects_migrated";
-lazy_static! {
- static ref IN_FLIGHT_BASES: tokio::sync::Mutex<HashSet<String>> =
- tokio::sync::Mutex::new(HashSet::new());
-}
-
pub fn migrate_if_needed(app: &AppHandle) -> Result<(), String> {
use tauri_plugin_store::StoreExt;
@@ -77,20 +72,24 @@ pub async fn migrate(app: &AppHandle) -> Result<(), String> {
tracing::debug!("Using concurrency limit of {}", concurrency_limit);
let wall_start = Instant::now();
+ let in_flight_bases = Arc::new(Mutex::new(HashSet::new()));
// (project_name, result, duration)
let migration_results = futures::stream::iter(uuid_projects)
- .map(|project_path| async move {
- let project_name = project_path
- .file_name()
- .map(|s| s.to_string_lossy().into_owned())
- .unwrap_or_else(|| project_path.display().to_string());
-
- let start = Instant::now();
- let res = migrate_single_project(project_path).await;
- let dur = start.elapsed();
-
- (project_name, res, dur)
+ .map(|project_path| {
+ let in_flight = in_flight_bases.clone();
+ async move {
+ let project_name = project_path
+ .file_name()
+ .map(|s| s.to_string_lossy().into_owned())
+ .unwrap_or_else(|| project_path.display().to_string());
+
+ let start = Instant::now();
+ let res = migrate_single_project(project_path, in_flight).await;
+ let dur = start.elapsed();
+
+ (project_name, res, dur)
+ }
})
.buffer_unordered(concurrency_limit)
.collect::<Vec<_>>()
@@ -181,7 +180,10 @@ enum ProjectMigrationResult {
Skipped,
}
-async fn migrate_single_project(path: PathBuf) -> Result<ProjectMigrationResult, String> {
+async fn migrate_single_project(
+ path: PathBuf,
+ in_flight_basis: Arc<Mutex<HashSet<String>>>,
+) -> Result<ProjectMigrationResult, String> {
let filename = path
.file_name()
.and_then(|s| s.to_str())
@@ -198,7 +200,7 @@ async fn migrate_single_project(path: PathBuf) -> Result<ProjectMigrationResult,
// Lock on the base sanitized name to prevent concurrent migrations with same target
let base_name = sanitize_filename::sanitize(meta.pretty_name.replace(":", "."));
{
- let mut in_flight = IN_FLIGHT_BASES.lock().await;
+ let mut in_flight = in_flight_basis.lock().await;
let mut wait_count = 0;
while !in_flight.insert(base_name.clone()) {
wait_count += 1;
@@ -211,7 +213,7 @@ async fn migrate_single_project(path: PathBuf) -> Result<ProjectMigrationResult,
}
drop(in_flight);
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
- in_flight = IN_FLIGHT_BASES.lock().await;
+ in_flight = in_flight_basis.lock().await;
}
if wait_count > 0 {
tracing::debug!(
@@ -225,7 +227,7 @@ async fn migrate_single_project(path: PathBuf) -> Result<ProjectMigrationResult,
let result = migrate_project_filename_async(&path, &meta).await;
- IN_FLIGHT_BASES.lock().await.remove(&base_name);
+ in_flight_basis.lock().await.remove(&base_name);
match result {
Ok(new_path) => {
diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs
index 4928f787f..e7d38edf2 100644
--- a/crates/utils/src/lib.rs
+++ b/crates/utils/src/lib.rs
@@ -1,7 +1,7 @@
use std::{
borrow::Cow,
future::Future,
- num::{NonZero, NonZeroI16, NonZeroI32},
+ num::{NonZero, NonZeroI32},
path::PathBuf,
sync::LazyLock,
};
@@ -56,7 +56,7 @@ pub fn ensure_unique_filename(
ensure_unique_filename_with_attempts(
base_filename,
parent_dir,
- // SAFETY: 20 is non zero
+ // SAFETY: 50 is non zero
unsafe { NonZero::new_unchecked(50) },
)
}
@@ -84,7 +84,7 @@ pub fn ensure_unique_filename_with_attempts(
(base_filename, String::new())
};
- let max_attemps = attempts.get();
+ let max_attempts = attempts.get();
let mut counter = 1;
loop {
@@ -103,7 +103,7 @@ pub fn ensure_unique_filename_with_attempts(
counter += 1;
// prevent infinite loop
- if counter > max_attemps {
+ if counter > max_attempts {
return Err(
"Too many filename conflicts, unable to create unique filename".to_string(),
);
@@ -111,10 +111,13 @@ pub fn ensure_unique_filename_with_attempts(
}
}
-/// Converts user-friendly moment template format strings to chrono format strings.
+/// Converts moment-style template format strings to chrono format strings.
///
-/// This function translates common template format patterns to chrono format specifiers,
-/// allowing users to write intuitive date/time formats that get converted to chrono's format.
+/// This function translates a custom subset of date/time patterns to chrono format specifiers.
+///
+/// **Note**: This is NOT fully compatible with moment.js. Notably, `DDD`/`DDDD` map to
+/// weekday names here, whereas in moment.js they represent day-of-year. Day-of-year and
+/// ISO week tokens are not supported.
///
/// # Supported Format Patterns
///