From b155e861adea1a29c5e4f9e717df8ff85a36052d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sun, 16 Oct 2022 18:12:26 +0900 Subject: [PATCH] Use a write_count to determine how many writes left to flush --- helix-view/src/editor.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index c0efad05e..ac0864e18 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -657,6 +657,7 @@ pub struct Editor { // https://stackoverflow.com/a/66875668 pub saves: HashMap>>, pub save_queue: SelectAll>>>, + pub write_count: usize, pub count: Option, pub selected_register: Option, @@ -761,6 +762,7 @@ impl Editor { documents: BTreeMap::new(), saves: HashMap::new(), save_queue: SelectAll::new(), + write_count: 0, count: None, selected_register: None, macro_recording: None, @@ -1206,6 +1208,8 @@ impl Editor { .send(stream::once(Box::pin(future))) .map_err(|err| anyhow!("failed to send save event: {}", err))?; + self.write_count += 1; + Ok(()) } @@ -1332,6 +1336,7 @@ impl Editor { biased; Some(event) = self.save_queue.next() => { + self.write_count -= 1; EditorEvent::DocumentSaved(event) } Some(config_event) = self.config_events.1.recv() => { @@ -1350,17 +1355,21 @@ impl Editor { } pub async fn flush_writes(&mut self) { - while let Some(save_event) = self.save_queue.next().await { - match &save_event { - Ok(event) => { - let doc = doc_mut!(self, &event.doc_id); - doc.set_last_saved_revision(event.revision); - } - Err(err) => { - log::error!("error saving document: {}", err); - } - }; - // TODO: if is_err: break? + while self.write_count > 0 { + if let Some(save_event) = self.save_queue.next().await { + match &save_event { + Ok(event) => { + let doc = doc_mut!(self, &event.doc_id); + doc.set_last_saved_revision(event.revision); + } + Err(err) => { + log::error!("error saving document: {}", err); + } + }; + // TODO: if is_err: break? + + self.write_count -= 1; + } } } }