From c70c72ef6172cd2b54703de3e120440bc5537bca Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 20 Apr 2022 21:54:31 +0200 Subject: [PATCH] eframe: make sure we wait for FileStorage to complete the save Closes https://github.com/emilk/egui/pull/1520 --- epi/src/file_storage.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/epi/src/file_storage.rs b/epi/src/file_storage.rs index 2fd72aa1..1c0af6cb 100644 --- a/epi/src/file_storage.rs +++ b/epi/src/file_storage.rs @@ -11,6 +11,15 @@ pub struct FileStorage { ron_filepath: PathBuf, kv: HashMap, dirty: bool, + last_save_join_handle: Option>, +} + +impl Drop for FileStorage { + fn drop(&mut self) { + if let Some(join_handle) = self.last_save_join_handle.take() { + join_handle.join().ok(); + } + } } impl FileStorage { @@ -21,6 +30,7 @@ impl FileStorage { kv: read_ron(&ron_filepath).unwrap_or_default(), ron_filepath, dirty: false, + last_save_join_handle: None, } } @@ -64,12 +74,19 @@ impl crate::Storage for FileStorage { let file_path = self.ron_filepath.clone(); let kv = self.kv.clone(); - std::thread::spawn(move || { + if let Some(join_handle) = self.last_save_join_handle.take() { + // wait for previous save to complete. + join_handle.join().ok(); + } + + let join_handle = std::thread::spawn(move || { let file = std::fs::File::create(&file_path).unwrap(); let config = Default::default(); ron::ser::to_writer_pretty(file, &kv, config).unwrap(); tracing::trace!("Persisted to {:?}", file_path); }); + + self.last_save_join_handle = Some(join_handle); } } }