Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Coalesce preview updates and atomic config write

Richie McIlroy4f3d312b1a2c
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index c2bc65af7..c17bd5aa4 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -2836,12 +2836,12 @@ async fn set_project_config(
editor_instance: WindowEditorInstance,
config: ProjectConfiguration,
) -> Result<(), String> {
+ editor_instance.project_config.0.send(config.clone()).ok();
+
config
.write(&editor_instance.project_path)
.map_err(|error| format!("Failed to write project config: {error}"))?;
- editor_instance.project_config.0.send(config).ok();
-
Ok(())
}
diff --git a/apps/desktop/src/routes/editor/Editor.tsx b/apps/desktop/src/routes/editor/Editor.tsx
index 15373fe6e..a916f5485 100644
--- a/apps/desktop/src/routes/editor/Editor.tsx
+++ b/apps/desktop/src/routes/editor/Editor.tsx
@@ -395,24 +395,50 @@ function Inner() {
return editorState.playbackTime;
});
- const doConfigUpdate = async (time: number) => {
- const config = getPreviewProjectConfig(project, editorState);
- const frameNumber = Math.max(Math.floor(time * FPS), 0);
- const resBase = previewResolutionBase();
+ type PreviewConfigUpdate = {
+ config: ReturnType<typeof getPreviewProjectConfig>;
+ frameNumber: number;
+ resolutionBase: ReturnType<typeof previewResolutionBase>;
+ };
+
+ let previewConfigUpdateInFlight = false;
+ let pendingPreviewConfigUpdate: PreviewConfigUpdate | null = null;
+
+ const flushPreviewConfigUpdate = async () => {
+ if (previewConfigUpdateInFlight) return;
+ const next = pendingPreviewConfigUpdate;
+ if (!next) return;
+
+ pendingPreviewConfigUpdate = null;
+ previewConfigUpdateInFlight = true;
+
try {
await commands.updateProjectConfigInMemory(
- config,
- frameNumber,
+ next.config,
+ next.frameNumber,
FPS,
- resBase,
+ next.resolutionBase,
);
} catch (e) {
console.error(
"[Editor] doConfigUpdate - ERROR sending config to Rust:",
e,
);
+ } finally {
+ previewConfigUpdateInFlight = false;
+ if (pendingPreviewConfigUpdate) void flushPreviewConfigUpdate();
}
};
+
+ const doConfigUpdate = (time: number) => {
+ pendingPreviewConfigUpdate = {
+ config: getPreviewProjectConfig(project, editorState),
+ frameNumber: Math.max(Math.floor(time * FPS), 0),
+ resolutionBase: previewResolutionBase(),
+ };
+ void flushPreviewConfigUpdate();
+ };
+
const throttledConfigUpdate = throttle(doConfigUpdate, 1000 / FPS);
const trailingConfigUpdate = debounce(doConfigUpdate, 1000 / FPS + 16);
const updateConfigAndRender = (time: number) => {
diff --git a/crates/project/src/configuration.rs b/crates/project/src/configuration.rs
index 00bd44415..5784ebb35 100644
--- a/crates/project/src/configuration.rs
+++ b/crates/project/src/configuration.rs
@@ -1,5 +1,4 @@
use std::{
- env::temp_dir,
fmt,
ops::{Add, Div, Mul, Sub, SubAssign},
path::Path,
@@ -1298,15 +1297,17 @@ impl ProjectConfiguration {
self.validate()
.map_err(|error| std::io::Error::new(std::io::ErrorKind::InvalidData, error))?;
- let temp_path = temp_dir().join(uuid::Uuid::new_v4().to_string());
+ let project_path = project_path.as_ref();
+ let config_path = project_path.join("project-config.json");
+ let temp_path =
+ project_path.join(format!(".project-config-{}.json.tmp", uuid::Uuid::new_v4()));
- // Write to temporary file first to ensure readers don't see partial files
std::fs::write(&temp_path, serde_json::to_string_pretty(self)?)?;
- std::fs::rename(
- &temp_path,
- project_path.as_ref().join("project-config.json"),
- )?;
+ if let Err(error) = std::fs::rename(&temp_path, &config_path) {
+ let _ = std::fs::remove_file(&temp_path);
+ return Err(error);
+ }
Ok(())
}