eframe: make sure we wait for FileStorage to complete the save
Closes https://github.com/emilk/egui/pull/1520
This commit is contained in:
parent
f789159a4a
commit
c70c72ef61
|
|
@ -11,6 +11,15 @@ pub struct FileStorage {
|
||||||
ron_filepath: PathBuf,
|
ron_filepath: PathBuf,
|
||||||
kv: HashMap<String, String>,
|
kv: HashMap<String, String>,
|
||||||
dirty: bool,
|
dirty: bool,
|
||||||
|
last_save_join_handle: Option<std::thread::JoinHandle<()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
impl FileStorage {
|
||||||
|
|
@ -21,6 +30,7 @@ impl FileStorage {
|
||||||
kv: read_ron(&ron_filepath).unwrap_or_default(),
|
kv: read_ron(&ron_filepath).unwrap_or_default(),
|
||||||
ron_filepath,
|
ron_filepath,
|
||||||
dirty: false,
|
dirty: false,
|
||||||
|
last_save_join_handle: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,12 +74,19 @@ impl crate::Storage for FileStorage {
|
||||||
let file_path = self.ron_filepath.clone();
|
let file_path = self.ron_filepath.clone();
|
||||||
let kv = self.kv.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 file = std::fs::File::create(&file_path).unwrap();
|
||||||
let config = Default::default();
|
let config = Default::default();
|
||||||
ron::ser::to_writer_pretty(file, &kv, config).unwrap();
|
ron::ser::to_writer_pretty(file, &kv, config).unwrap();
|
||||||
tracing::trace!("Persisted to {:?}", file_path);
|
tracing::trace!("Persisted to {:?}", file_path);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
self.last_save_join_handle = Some(join_handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue